1.1 Decorative Device
A function object can be assigned to a variable, so the function can also be called by a variable.
>>> def now ():
.. print (' 2016 ')
...
>>> Now ()
2016
>>> F = Now--The function object assigns a value to the variable
>>> f ()-- call
2016
of the Function object __name__ , can get the name of the function
>>> now.__name__
' Now '
>>> f.__name__
' Now '
Suppose we want to enhance the function of the now () function, for example, to automatically print the log before and after a function call, but do not want to modify the definition of the now () function, a way to dynamically add functionality during the run of the code, called an "adorner" ( Decorator).
Decorator is a higher-order function that returns a function . Define a decorator as follows
>>> def log (func):
... def wrapper (*args, **kw):-- the previous variable parameter and keyword Parameters section has been explained,= can pass any parameter
... print (' Call%s (): '% func.__name__)
... return func (*args, **kw)
... return Wrapper
...
>>> @log-- equivalent to executing now = log (now), redefining the now function.
... def now ():
.. print (' 2016 ')
...
>>> Now ()
Call Now ():
2016
#################################################################
>>> def log2 (func):
... def wrapper (*args, **kw):
... print (' Begin call ')-- execute before calling
... res = func (*args, **kw)
... print (' End call ')-- execution after invocation
... return Res
... return Wrapper
>>> @log2
... def PR ():
.. print (' 2016 ')
...
>>>
>>> PR ()
Begin Call
2016
End Call
##################################################################
>>> def log (text):-- custom text content
... def decorator (func):
... def wrapper (*args, **kw):
... print ('%s%s (): '% (text,func.__name__))
... return func (*args, **kw)
... return Wrapper
... return Decorator
...
>>>
>>>
>>>
>>> @log (' execute ')-- equivalent to execute now=log (' Execute ') (now)
... def now ():
.. print (' 2016 ')
...
>>> Now ()
Execute Now ():
2016
>>> now.__name__
' Wrapper '-- returns the function name, but wrapper
##################################################################
>>> Importfunctools
>>>
>>> def log (func):
... @functools. Wraps (func)
... def wrapper (*args, **kw):
... print (' Call%s (): '% func.__name__)
... return func (*args, **kw)
... return Wrapper
...
>>>
>>>
>>> @log
... def now ():
.. print (' 2 ')
...
>>> now
<function now at 0x2ab1090d9e18>
>>> Now ()
Call Now ():
2
>>> now.__name__
' Now '
##################################################################
>>> Importfunctools
>>> def log (text):
... def decorator (func):
... @functools. Wraps (func)
... def wrapper (*args, **kw):
... print ('%s%s (): '% (text,func.__name__))
... return func (*args, **kw)
... return Wrapper
... return Decorator
...
>>> @log (' Run ')
... def now ():
.. print (' 2016 ')
...
>>> Now ()
Run Now ():
2016
>>> now.__name__
' Now '
just remember to add @functools. Wraps (func) in front of the definition wrapper () .
This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1820989
Python functional Programming--decorators