Short functions like those you just saw can also be defined using lambda expressions of the form. Lambda expressions are anonymous little functions that you can quickly define inline. Often, a small function needs to be passed to another function, like the key function used by a list's sort method. In such cases, a large function is usually unnecessary, and it would be awkward to have to define the function in a separate place from where it's used. This defines lambda expressions as values of the dictionary. Note that lambda expressions don't have a return statement, because the value of the expression is automatically returned.
A generator function is a special kind of function that you can use to define your own iterators. When you define a generator function, you return each iteration's value using the yield keyword. When there are no more iterations, an empty return statement or flowing off the end of the function ends the iterations. Local variables in a generator function are saved from one call to the next, unlike in normal functions. Note that this generator function has a while loop that limits the number of times the generator will execute. Depending on how it's used, a generator that doesn't have some condition to halt it could cause an endless loop when called. You can also use generator functions with in to see if a value is in the series that the generator produces. Because functions are first-class objects in Python, they can be assigned to variables, as you've seen. Functions can be passed as arguments to other functions and passed back as return values from other functions. For example, it's possible to write a Python function that takes another function as its parameter, wrap it in another function that does something related, and then return the new function. This new combination can be used instead of the original function.
A decorator is syntactic sugar for this process and lets you wrap one function inside another with a one-line addition. This still gives you exactly the same effect as the previous code, but the resulting code is much cleaner and easier to read. Very simply, using a decorator involves two parts: defining the function that will be wrapping or "decorating" other functions and then using an @ followed by the decorator immediately before the wrapped function is defined. The decorator function should take a function as a parameter and return a function. The decorate function prints the name of the function it's wrapping when the function is defined. When it's finished, the decorator returns the wrapped function. myfunction is decorated using @decorate.
The wrapped function is called after the decorator function has completed. Using a decorator to wrap one function in another can be handy for a number of purposes. In web frameworks such as Django, decorators are used to make sure a user is logged in before executing a function; and in graphics libraries, decorators can be used to register a function with the graphics framework. Defining functions in Python is simple but highly flexible. Although all variables created during the execution of a function body are local to that function, external variables can easily be accessed using the global statement. Python functions provide exceedingly powerful argument-passing features: Arguments may be passed by position or by parameter name. Default values may be provided for function parameters. Functions can collect arguments into tuples, giving you the ability to define functions that take an indefinite number of arguments. Functions can collect arguments into dictionaries, giving you the ability to define functions that take an indefinite number of arguments passed by parameter name.
Functions are first-class objects in Python, which means that they can be assigned to variables, accessed by way of variables, and decorated. Functions are essential building blocks for writing readable, structured code. By packaging code that performs a particular function, they make reusing that code easier, and they also make the rest of your code simpler and easier to understand.
Our website is not responsible for the information contained by this article. Articleinput.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.
Note: This article was sent to us by: Roger Janssen at 01052010
1. What can be used as a key in Python
All articles are property of their respective authors. Please read our Privacy Policy!
© 2009 ArticleInput.com.