What is an idiot understanding of python decorators?

Source: Internet
Author: User

What is an idiot understanding of python decorators?

No parameter decorator

For python, the decorator of python is awesome. I don't know how to understand it. Actually, according to the literal meaning of the decorator,

It is to decorate the defined function and then return a new function (note that it is new, and it is no longer a function originally defined)

In this case, let's take a look at the Code:

1 # decoration function 2 def decorator (foo): 3 def wrapper (): 4 print 'wrapper' 5 return foo () 6 return wrapper 7 8 # custom function 9 def abc (): 10 print 'abc' 11 12 # decoration abc13 abc = decorator (abc)

The above is the decorator process. We can see that the wrapper function object is returned when the decorator function is called, rather than the abc function object,

The abc function has been called in wrapper, but returns a result. The running result of the above Code is shown in.

Then abc is the function object of wrapper, not the original function.

The python syntax sugar '@' is to execute the above process. The following code is the same as the above Code:

1 def decorator(foo):2     def wrapper():3         print 'wrapper'4         return foo()5     return wrapper6 7 @decorator8 def abc():9     print 'abc'

The decorator Syntax of python is to automatically call the decorator function and take the custom function abc function object as the parameter,

Returns the wrapper function object.

Decorator with parameters:

Next, let's talk about the parameter-based decorators. In fact, this is only one step more than the parameter-free decorators above,

The decorator function is called first, and then the real decorator is returned. The subsequent steps are the same as those without parameters. Is it too abstract?

Directly add the code, so it becomes obvious that the function with the outermost layer is called first, and a real decorator is returned, and then

If no parameter is set before, the abc function is modified and a new function object is returned.

 1 def decoratorFunc(arg): 2     def decorator(foo): 3         def wrapper(): 4             if arg == 0: 5                 print 'lalala' 6             return foo() 7         return wrapper 8     return decorator 9 10 deco0 = decoratorFunc(0)11 deco1 = decoratorFunc(1)12 13 abc0 = deco0(abc)14 abc1 = deco1(abc)

Then the python syntax sugar process is like the above, so I come to the conclusion that @ must be followed by

Function object, not function call!

1 @decoratorFunc(0)2 def abc():3     print 'abc'

Last

Write a more common decorator. What if the decorated function has parameters? It's easy to use python variable length parameters.

Note that the variable length parameter of python is written on the wrapper function, while the decorator function parameter

There will always be only one function object. See the following code for an example without a parameter modifier. If it is a function with a parameter

You only need to change wrapper to receive variable length parameters.

 1 def decorator(foo): 2     def wrapper(*args, **kwargs): 3         print 'wrapper' 4         return foo(*args, **kwargs) 5     retrun wrapper 6  7  8 @decorator 9 def abc(arg):10     print 'abc:', arg

 

 

Author: Chen dongquan

Time: 2016/09/05

The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent,

The original Article connection is clearly provided on the Article Page, otherwise the right to pursue legal liability will be reserved.

If you have special purpose, please contact me: kingchen.gd@foxmail.com

 

Finally, if you are interested, you can follow my public account and read my article at any time. * ^_^ *

Scan the QR code to follow or search for the number King_diary.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.