0. Description
Before you can summarize and decompose the execution process of the Python decorator, for the adorner, although it understands the basic way of working, but for adorners with complex parameters (the adorner and the function itself have parameters), it will always be very vague, even though it will be understood, and the next time soon forget, In fact, there is still not much time to understand the details of the problem.
Although there are many such articles on the Internet, it is obvious that they are the thoughts of others, so they are always hard to remember, so take some time to tidy them up.
Recently in the "Python core programming" to do a summary, a lot of harvest, below to share my own understanding of the Python decorator, the following also provides a more complex Python decorator execution process decomposition, you can refer to.
Appearance of the 1.Python adorner
Before you can define a static method in a class without an adorner, you need to use the following method:
Class MyClass (object): Def staticfoo (): Staticfoo = Staticmethod (Staticfoo)
It is obviously cumbersome to add a staticmethod () built-in function to the static method to convert the method to a static method, which, after having the adorner, can be written as follows:
Class MyClass (object): @staticmethod def staticfoo (): Pass
This is a lot more concise.
2.Python Decorator Type and understanding
(1) No parameter adorner
The following conditions:
@fdef foo (): Pass
is actually equivalent to:
def foo (): Passfoo = g (foo)
The following conditions:
@g@fdef foo (): Pass
is equivalent to:
def foo (): Passfoo = g (f (foo))
(2) with parametric adorner
The following conditions:
@decomaker (Deco_args) def foo (): Pass
is equivalent to:
def foo (): Passfoo = Decomaker (Deco_args) (foo)
Understanding it with this idea is very good:Decomaker () did something with Deco_args and returned the function object, which is the adorner with Foo as its argument .
The following examples of adorners are also based on the idea to understand.
The following conditions:
@deco1 (Deco_arg) @deco2 () def foo (): Pass
is equivalent to:
def foo (): Passfoo = Deco1 (Deco_arg) (Deco2 (foo))
Manual decomposition of the 3.Python adorner execution process
OK, with the theoretical basis above, it is easy to understand the following more complex adorner:
From functools import wrapsdef log (text): def decorator (func): @wraps (func) #it works like:wraper.__name __ = func.__name__ def wrapper (*args, ** Kwargs): print '%s %s (): ' % (text, func.__name__) return func (*args, **kwargs) return Wrapper return decorator@log (' Hello ') def now (area): print area, ' 2016-01-23 ' now (' Beijing ') print ' The name of function now () is: ', now.__name__
EXECUTE as follows:
/usr/bin/python2.7/home/xpleaf/pycharmprojects/decorator_test/dec10.pyhello now (): Beijing 2016-01-23The Name of function now () Is:now
For the execution of the program, you can analyze the following: 1. The log (' Hello ') function is executed first, and a new function is returned, except that the text variable is replaced with ' hello ', so the new adorner used to decorate the now function is as follows: def decorator (func): @wraps (func) #it works like:wraper.__name__ = func.__name__ def wrapper (*arg S, **kwargs): print '%s%s (): '% (' Hello ', func.__name__) return func (*args, **kwargs) return wrapper
2. So the now function is the equivalent of: now = Decorator (now)
3. Now is the equivalent of: def now (*args, **kwargs): print '%s%s (): '% (' Hello ', old_now.__name__) return Old_now (*args, **kwargs) # The function name changed to Now instead of wrapper because of the use of the wraps adorner
So, the result of the output is very well understood. |
About wraps, it is also an adorner, using its function is that we use the custom adorner modified functions, its function name, that is, func.__name__ is the same as the original, and it works as mentioned above, namely:
wraper.__name__ = func.__name__
In other words, the use of wraps can not change the original function of the properties, of course, the above is simply a brief description of how it works, detailed can refer to wraps source code.
There are 10 examples of how to understand adorners on GitHub, so take a look: Https://github.com/xpleaf/decorator
This article selected "Python Review and Collation 9: Functional and functional Programming" in my "Python Review and Collation" series blog
This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1763567
A personal understanding of the Python decorator