The @ modifier python

'@' Has been cited as a function of the following function modified. Modified reference function must be placed above function, the return value of the reference function, the function returns to the modified

A simple chestnuts:

. 1  DEF FUNA (fn):
 2      Print ( ' A ' ) output # A
 . 3      fn () # perform incoming parameter output fn B 
. 4      return  ' fkit ' returns to # funB 
. 5  '' ' 
. 6  below the decorative effect is equivalent to: FUNA (funB),
 . 7  funB will replace (decorative) as the return value of the statement;
 8  due FUNA () function returns fkit, so that funB fkit
 . 9  '' ' 
10  @funA
 . 11  DEF funB ():
 12 is      Print ( ' B ' )
 13 is  Print (funB) # then re-run output function is fkit funB

 

 Here is funA function references, funB is modified function. @ Modifier function is passed as parameters to funB funA, and then returns the return value to FUNA funB

This means that the procedure to complete two steps:

The funB parameters as FUNA (), which is equivalent to executing the code above @funA funA (funB).

Alternatively to the result at step funB execution, FUNA () After completion of the execution returns fkit, so it is no longer funB function, but is replaced by a string.

Output is as follows:

A
B
Fkit

 

Of course, you can also return a function, see the following chestnuts:

. 1  DEF My_Decorator (FUNC):
 2      DEF warpper ():        # skip function 
. 3          '' ' Decorator ' '' 
. 4          Print ( ' Decorated function ... ' )
 . 5      Print ( ' 123 ' )
 . 6      return warpper   # the wrapper function returns Test 
. 7  
. 8  
. 9 @my_decorator   # Decorator (Test ()) 
10  DEF Test ():
 . 11      "" " the Testword " ""
12     print(' The Test function ' )
 13 is  
14  
15  Test ()
 16  Print ( " function name:% S " . Test% the __name__ ) 

output is as follows:
123
Decorated function ...
function name: wrapper

 

 

As can be seen, the first output of the fifth line of '123', then the fourth row ' Decorated function ...', and finally the 16th line of the function name, the code following stroke order of execution:

9 the first line of code is executed, it runs the referenced function, the reference function is the return value inside the nested function warpper, skip this nested function, the line 5 runs, then the output '123'; run sixth line to test the nested function returns the function, then the role of @ modifier even been completed, the next code run line 15, call the test function, but this time did not test inside

 

' The Test function ' , the second is the inside of the wrapper output ' Decorated function ... ' , which is to be referenced as a function of the effect of the return value of the function, the nature of the test at this time is no longer a test, the second is cited function's return value wrapper function, so the output is content inside the wrapper function. Finally, we verify the name of the output function. 

This is the role of the @ modifier, not flying chicken dishes, the article feel free to correct me chiefs have welcomed the error.
----------------------------------------- effort, not just a habit, and more It is a quality.

 

Guess you like

Origin www.cnblogs.com/liangxiyang/p/11200476.html