Python adorner, divided into two parts, one is the definition of the adorner itself, one is the definition of the adorner object.
A functional decorator: the adorner itself is a function.
1. Decorative function: The object being decorated is a function
[1] Adorner no parameters:
A. The adorned object has no parameters:
B. The decorated object has parameters:
[2] The adorner has parameters:
A. The adorned object has no parameters:
>>> def test (printresult=false):d ef _test (func):d EF __test ():p rint ' Call the function%s (). %func.func_nameif printresult:print func () Else:return func () return __testreturn _test>>> @test (True) def say ( ): Return ' Hello World ' >>> say () the function say (). Hello world>>> @test (False) def say (): Return ' Hello World ' >>> say () Call the function say (). ' Hello World ' >>> @test () def say (): Return "Hello World" >>> say () Call the function say (). ' Hello World ' >>> @testdef Say (): Return "Hello World" >>> Say () Traceback (most recent call last): File "<pyshell#224>", line 1, in <module>
The last two examples from the above code show that when the adorner has parameters, even if you enable the adorner's default parameters, and no additional values are passed in, there must be a pair of parentheses, otherwise the compiler will pass Func directly to test () instead of passing it to _test ()
B. The decorated object has parameters:
>>> def test (printresult=false):d ef _test (func):d EF __test (*args,**kw):p rint ' Call the function%s (). ' %func.func_nameif printresult:print func (*args,**kw) Else:return func (*args,**kw) return __testreturn _test>> > @test () def left (Str,len): #The parameters of __test can is ' (Str,len) ' in the case. Return str[:len]>>> left ("Hello World", 5) Call the function left (). ' Hello ' >>> @test (True) def left (Str,len): #The parameters of __test can is ' (Str,len) ' in the case.
2. Decoration class: The object being decorated is a class
[1] Adorner no parameters:
A. The adorned object has no parameters:
B. The decorated object has parameters:
>>> def Test (CLS):d EF _test (*args,**kw): Clsname=re.findall (' (\w+) ', repr (CLS)) [ -1]print ' Call%s.__init (). ' %clsnamereturn CLS (*args,**kw) return _test>>> @testclass Sy (object):d EF __init__ (self,value):
[2] The adorner has parameters:
A. The adorned object has no parameters:
B. The decorated object has parameters:
Second, class decorator: The adorner itself is a class, borrowing __init__ () and __call__ () to achieve the function
1. Decorative function: The object being decorated is a function
[1] Adorner no parameters:
A. The adorned object has no parameters:
B. The decorated object has parameters:
[2] The adorner has parameters
A. The adorned object has no parameters:
Or:
B. The decorated object has parameters:
Or:
2. Decoration class: The object being decorated is a class
[1] Adorner no parameters:
A. The adorned object has no parameters:
B. The decorated object has parameters:
[2] The adorner has parameters:
A. The adorned object has no parameters:
B. The decorated object has parameters:
Summary: When the "1" @decorator is followed by no parentheses (that is, when the adorner has no parameters), the effect is equivalent to defining the Func or CLS before performing the assignment operation Func=decorator (func) or Cls=decorator (CLS);
When the "2" @decorator is followed by parentheses (that is, when the adorner has parameters), the effect is equivalent to defining the Func or CLS before performing the assignment operation Func=decorator (Decoratorargs) (func) or Cls=decorator ( Decoratorargs) (CLS);
"3" When the Func or CLS is re-assigned, the Func or CLS at this time is no longer the Func or CLS as originally defined, but an executable, you only need to pass in parameters to invoke, func (args) = return value or output, cls (args) = Object of the CLS;
"4" Finally the execution body returned by the assignment is varied, can be a closure or an external function, and when it is decorated with a class, it can also be a class internal method, function;
"5" In addition to really understand the adorner, be sure to understand func.func_code.co_varnames,func.func_defaults, through which you can restore Func's argument list beyond the definition of func In addition, the keyword parameter is the result of the invocation, and not because of the Func definition, in the definition of Func, only the parameters with the default values are concatenated, they are not necessarily the keyword arguments, because you can still pass them by location.
Python all kinds of decorative device