Introduction to Adorners:
An adorner (decorator) is an advanced Python syntax. Adorners can be used to process a function, method, or class. In Python, we have several methods for working with functions and classes, such as in Python closures, where we see the function object as the result of a function's return. With respect to other methods, the adorner has a simple syntax and a code with high readability. As a result, adorners are widely used in Python projects.
The adorner first appeared in Python 2.5, which was originally used to process functions and methods such as callable objects (callable object, which defines the __call__ method). In Python 2.6 and later versions of Python, adorners are further used for processing classes.
Adorners are mainly used for packaging functions, for some common functions, such as: Log printing, function timing, identity authentication. We can use adorners to achieve this, which reduces the complexity of the entire program and reduces the amount of code in the program.
It is actually a function, but the difference is that it considers a function as an argument and then returns an alternate version of the function.
Let's look at a simple example:
def add_number (func):d EF Adder (ARG): return func (ARG) +100return adderdef f (x): Return Xf=add_number (f) Print F (20)
Add_number is an adorner function that takes a function (f) as a parameter, and then returns another function (adder) to the original function, so that the original function does not add the additional code amount to the new addition function.
This is the original implementation of the adorner.
But, this way is still a bit inconvenient, after all, still around a circle, with F=add_number (f) to the original function to re-assign value.
In fact, Python can simplify the reference to adorners in the following ways.
def add_number (func):d EF Adder (ARG): return func (ARG) +100return adder@add_numberdef f (x): Return xprint F (20)
Just a simple @add_numbe call, is not convenient, a lot simpler, basically did not invade the original code.
Well, we found no, as an adorner, each parameter accepted is nothing more than two: function and function parameters, but the writing format is basically the same, there is no way to simplify this kind of writing?
Yes, Python provides a decorator package that can greatly simplify the writing of adorners.
So, the third way to achieve this is:
From decorator import Decorator@decoratordef wrapper (Func,arg): return func (ARG) +100@wrapperdef f (x): Return xprint F (20 )
Oh, it's even easier.
The above example accepts a parameter, in fact, the function itself can accept variable parameters.
Such as:
@decoratordef Wrapper (F,arg1,*args,**kwargs):p rint "I am just a wrapper~" return F (Arg1,*args,**kwargs) @wrapperdef F ( Arg1,*args,**kwargs):p rint arg1for eachearg in Args:print ' Non-keyword arg: ', Eacheargfor eachkw in Kwargs.keys ():p rint ' Keyword arg:%s:%d '% (eachkw,kwargs[eachkw]) args= (' Joy ', ' Steve ') kwargs={"age": 20}f (' China ', *args,**kwargs)
The output is:
I am just a wrapper~chinanon-keyword arg:joynon-keyword arg:stevekeyword arg:age:20
For the difference between *args,**kwargs, both can be used to represent variable length parameters. But the former is expressed in Ganso, there is no key value, the latter is a dictionary, there is a key value. Both can be used in the same function, but *args must appear before **kwargs.
such as the following example:
def test_var_args_call (Arg1, arg2, Arg3):p rint "arg1:", Arg1print "arg2:", Arg2print "Arg3:", arg3args= (all in a) Kwargs ={" Arg1 ":" 1 "," Arg3 ": 3," arg2 ":" 2 "}test_var_args_call (*args) print '-----------------' Test_var_args_call (**kwargs)
The two achieve the same effect.
Finally, an example is to decorate a function by showing the time the function executes
Import timedef Log (func):d EF Wrapper (*args, **kw):p rint ' [%s]%s () was called ... '% (Time.ctime (), func.__name__) return fu NC (*args, **kw) return wrapper@logdef foo ():p assfor i in range (4): foo () time.sleep (1)
The output results are as follows:
[Wed Jul 09:17:23] Foo () was called ... [Wed Jul 09:17:24] Foo () was called ... [Wed Jul 09:17:25] Foo () was called ... [Wed Jul 09:17:26] Foo () was called ...
The above is a small series to introduce you to the in-depth understanding of Python decorator, I hope that we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for your support for topic.alibabacloud.com!