1. Preface
The function is also an object, which allows you to increment the property and use a period to represent the property.
If the definition of an intrinsic function contains a reference to an object defined in an external function (the outer object can be outside the external function), then the intrinsic function is called a closure.
2, the decorative device
Adorners are wrapping the original functions, so that you can do more things without modifying the original code.
The adorner syntax is as follows:
@deco2 @deco1def func (arg1,arg2 ...): Pass
This represents a function with two adorners, so the meaning of this expression is: Func = Deco2 (Deco1 (func))
The non-parametric adorner syntax is as follows:
@decodef func (): Pass
The meaning of the representation is Func = Deco (func)
The syntax for the reference adorner is as follows:
@deco (Deco_args) def func (): Pass
The meaning of the expression is: Func = Deco (Deco_args) (func)
Several adorners have parameters and no parameters, the syntax is as follows:
@deco2 (ARG1,ARG2) @deco1def func (): Pass
The meaning of the expression is Foo = Deco2 (arg1,arg2) Deco1 (func)
An adorner is actually a function, and a function object is accepted.
Before you execute a function, you can run the prep code, or you can do some cleanup after executing the code.
When you see the adorner, it's possible to find some code in it that defines a function and embeds a call to the target function somewhere within the definition, or at least a point reference.
3, the role of the adorner
The role of adorners is as follows:
Introducing logs; adding timing logic to detect performance; the ability to add things to a function
The following example shows the detection of performance for the introduction of timing logic, as follows:
#!/usr/bin/env pythonfrom Time Import ctime,sleepdef Tsfunc (func): #装饰器函数接受的是一个函数对象 def wrappedfunc (): print ' [%s]%s () called '% (CTime (), func.__name__) return func () #在这里调用了函数对象, that is, the adorner is decorated with the original function, so that the original function on the basis of a number of operations return Wrappedfunc@tsfunc #装饰器def foo (): Passfoo () sleep (4) for I in Range (2): sleep (1) foo ()
The results of the implementation are as follows:
[Root@python 420]# python deco.py [Tue Apr 16:15:01] foo () called[tue Apr 16:15:06] foo () called[tue Apr 1 9 16:15:07] Foo () called
In the adorner function, the timestamp is added and the target function is called, and the return value of the adorner is a wrapper function .
4. Closed Package
If, in an intrinsic function, a reference is made to a variable that is acting externally (but not the global scope), the intrinsic function is considered a closure closure, and the variable defined in the outer function but referenced or used by an intrinsic function is called a free variable.
The main functions of closures are as follows:
Install calculation; hide state; switch freely in function objects and scopes.
Callbacks are functions, closures are functions, but can carry a little extra scope.
#!/usr/bin/env pythondef Counter (start_at=0): count = [Start_at] def incr (): count[0] + = 1 return Count[0] return incrcount= counter (5) print count () print count ()
As you can see in the example above, the function inside the function incr references the external variable count and adds one each time, so that the variable count is called a free variable.
Above this Python function summary of the decorator closure is a small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we support the script home.