This week learned the Python decorator, had not contacted before, asked a colleague of PHP development what is an adorner, he said it is like a constructor, but I have forgotten the structure of the light, I can not remember what is. Find out more about the information now. After all, the adorner is a function that does not have the ability to implement the program, but with the adorner will appear your technical special NB, but not very good understanding
Learn the decorator before, you need to understand the function, the previous notes, there are written functions related notes, you can first refer to the previous article, here only briefly.
In Python, a function consists of a def keyword, a function name, an optional argument list, and a function body that returns a value by a return statement that automatically returns a value of None if there is no return statement.
The adorner is actually taking a function as an argument, and then returning an alternate version of the function,
def wrapper (A_fun): def fun (): print "a fun" TMP = A_fun () return TMP +1 return fundef w_test () : Return 1var1 = wrapper (w_test) print var1 () #1print w_test ()
Output:
A fun # var1 () output
2 # var1 () output
1 # w_test () output
Above is an example of a decorator, we define a function wrapper (), the parameter is a function called A_fun, inside the function defines a nested function fun (). Fun () will output a string of strings, then call A_fun (), TMP can get the return value of A_fun (), wrapper each call fun () The value may be different (if you are using other functions), but regardless of the return value of A_fun (), it will be called outside , the last Fun () return value is A_fun () +1. In the position #1处, we call the function stored in the variable var1 to get the printed string and return value 2 instead of the return value of the function W_test ().
We can assume that var1 is a decorative version of the function w_test (), or a modified version, and that there is no change to the original function w_test (). To write an adorner, the outside can be completely replaced with a decorative version of the original function w_test (), so you can get a modified version of the function W_test (), in the course of use, do not need to learn the new syntax, as long as the simple copy of the variable can achieve the effect
W_test = wrapper (w_test) w_test output: <function fun at 0x7f5edf6c5668>
After executing the above code, the outside call will not be involved in the original function w_test (), the call would be a modified version of the W_test ()
Let's take another concrete example.
Wrapper (func): Result (): Func () Resultfoo (): foo = wrapper (foo) # # # here foo ()
Applying an adorner to a function using the @ identifier
python2.4 supports the use of identifiers to speak of adorners applied to functions, simply to define the function in the money called @ and the name of the adorner,
In the example above, the method used outside is
foo = wrapper (foo)
This method can be wrapped at any time in any way, but the code is much, tired. We can use the simple method, using the @ decoration, the above example modified:
Wrapper (func): Result (): Func () Result@foo (): foo ()
Keep in mind that using Foo = wrapper (foo) and adding @wrapper before the function are the same, Python just adds some syntactic sugars, making the adornment behavior more straightforward and elegant, and, frankly, making it easier for you to see the code.
About passing parameters
We have completed an adorner above, but this adorner can only be applied to a specific method. What if we want to implement an adorner that can be applied to any method?
You can actually guess part of the use of *args and **kwargs.
Cases:
First (*args): Argsfirst () First (,,) lst = [,,,]first (*LST)
The above defines the function first, prints any parameters passed in, * indicates the method to be called, the additional parameters can be obtained from an iterative list, or when the method is defined to indicate that the parameter can receive any number of positional parameters
* * Slightly more complicated than *, when using * *, the corresponding is a parameter dictionary that represents the key value pairs.
Cases:
SEC (**kwargs): Kwargsdic = {:,:}sec () sec (=,=) sec (**dic)
Let's write a more powerful decorator
War (func): In_war (*args,**kwargs):% (Args,kwargs) in_war@foo_1 (x,y=): X+y@foo_2 (): Foo_1 (,) foo_ 1 () Foo_2 ()
http://timesnotes.blog.51cto.com/1079212/1715330
http://www.timesnotes.com/?p=111
This article is from the "'ll Notes" blog, so be sure to keep this source http://timesnotes.blog.51cto.com/1079212/1715330
Python Learning note-day04-part fourth (decorator)