Reviewing functions
Because it is important to understand how functions work when we deal with decorators, we'll take a quick look at them. First, we need to remember that everything in Python is an object, including functions.
Functions are created in Python by using the def
keyword and naming the function; input arguments are optional. Following is a basic function for reference:
def func_foo(): pass
How to do it...
- Functions can have multiple names, that is, in addition to the function name itself, the function can be assigned to one or more variables. Each name has the same capabilities of the underlying function:
>>> def first_func(val): ... print(val) ... >>> new_name = first_func >>> first_func("Spam!") Spam! >>> new_name("Spam too!") Spam too!
- Functions can be used as arguments for other functions. Some Python built-in functions, such as
map
andfilter
, use this feature to do their jobs:
>...