Home >Backend Development >Python Tutorial >Detailed explanation of closures and decorators in Python
Closure is an important grammatical structure in functional programming. Closure is also a structure for organizing code, which also improves the reusability of code.
If in an inline function, a variable in the outer function (but not in the global scope) is referenced, then the inline function is considered a closure.
Variables defined within an external function but referenced or used by an internal function are called free variables.
To summarize, creating a closure must meet the following points:
1. There must be an embedded function
2. The embedded function must reference the variables in the external function
3. The return value of the external function must be the embedded function
Let’s first look at an example of closure:
In [10]: def func(name): ...: def in_func(age): ...: print 'name:',name,'age:',age ...: return in_func ...: In [11]: demo = func('feiyu')In [12]: demo(19) name: feiyu age: 19
Here, when func
is called, a closure is generated——in_func
, and the closure holds the free variable - name
, so this also means that when the life cycle of the function func
ends, name
This variable still exists because it is referenced by the closure, so it will not be recycled.
In the function of python
, you can directly reference external variables, but you cannot rewrite external variables. Therefore, if you directly rewrite the variables of the parent function in the closure, an error will occur. Look at the following example:
Example of implementing a counting closure:
def counter(start=0):count = [start] def incr():count[0] += 1return countreturn incr a = counter() print 'a:',aIn [32]: def counter(start=0): ...: count = start ...: def incr(): ...: count += 1 ...: return count ...: return incr ...: In [33]: a = counter()In [35]: a() #此处会报错 UnboundLocalError: local variable 'count' referenced before assignment
should be used like the following:
In [36]: def counter(start=0): ...: count = [start] ...: def incr(): ...: count[0] += 1 ...: return count ...: return incr ...: In [37]: count = counter(5) In [38]: for i in range(10): ...: print count(), ...: [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]
In [1]: def create(): ...: return [lambda x:i*x for i in range(5)] #推导式生成一个匿名函数的列表 ...: In [2]: create()Out[2]: [<function __main__.<lambda>>, <function __main__.<lambda>>, <function __main__.<lambda>>, <function __main__.<lambda>>, <function __main__.<lambda>>]In [4]: for mul in create(): ...: print mul(2) ...: 88888
Isn’t the result very strange? This is a trap in the use of closures! Let’s see why?
In the above code, the function create
returns a list
which contains 4 function variables. These four functions all reference the loop variablei
, that is to say, they share the same variable i
, i
will change. When the function is called, the loop variable i
has been It is equal to 4, so all four functions return 8. If you need to use the value of a loop variable in a closure, use the loop variable as the default parameter of the closure or implement it through a partial function. The implementation principle is also very simple, that is, when the loop variable is passed into the function as a parameter, new memory will be applied for. The sample code is as follows:
In [5]: def create(): ...: return [lambda x,i=i:i*x for i in range(5)] ...: In [7]: for mul in create(): ...: print mul(2) ...: 02468
The decorator is a kind of closure application, but it passes a function:
def addb(func):def wrapper():return '<b>' + func() + '</b>'return wrapperdef addli(func):def wrapper():return '<li>' + func() + '</li>'return wrapper @addb # 等同于 demo = addb(addli(demo)) @addli # 等同于 demo = addli(demo)def demo():return 'hello world' print demo() # 执行的是 addb(addku(demo))
During execution, the demo
function is first passed to addli
for decoration, and then the decorated function is passed to addb
for decoration. So the final result returned is:
<b><li>hello world</li></b>
When you write a decorator that acts on a function, the important meta-information of this function such as its name , docstrings, annotations, and parameter signatures will be lost.
def out_func(func):def wrapper(): func()return wrapper@out_funcdef demo():""" this is a demo. """print 'hello world.'if __name__ == '__main__': demo()print "__name__:",demo.__name__print "__doc__:",demo.__doc__
Look at the results:
hello world.__name__: wrapper__doc__: None
The function name and documentation string have become closure information. Fortunately, you can use the @wraps
decorator in the functools
library to annotate the underlying wrapper function.
from functools import wrapsdef out_func(func): @wraps(func)def wrapper(): func()return wrapper
Try the results yourself!
The above is the detailed content of Detailed explanation of closures and decorators in Python. For more information, please follow other related articles on the PHP Chinese website!