python base trim is decorative function without parameter principle Explanation

1  Import Time
 2  # Composition 1 decorator decorator, i.e. a function of 2 variables, higher-order functions 3, three nested function knowledge 
3  # variable is a variable address variable name, exist in the memory space corresponding to the memory address that is a function of the variable content the most typical examples of variables: anonymous function 
. 4 CAL = the lambda X: X ** 2 # anonymous function corresponds to the function definition itself, the assignment statement is equivalent to the function assigned to a variable definition statement body, equivalent functions; so anonymous function corresponding to the variable assignment required like a function with () call 
. 5  Print (CAL (. 3 ))
 . 6  # function storage in memory: the memory address corresponding to the function thereof can be addressed to a function name, memory can be considered a string stack body functions, the function name () represents a function name corresponding to the implementation of the code in memory. 
7  # function that is variable, the function call statement is defined inside thereof can function when no function is defined inside this function call before; i.e., not defined in claim test2 test1 before, only the first two memory function body memory, test1 when calling () the specific implementation of the corresponding sentence 
8  DEF test1 ():
 9      Print ( " the this IS test1" )
 10      test2 ()
 . 11  DEF test2 ():
 12 is      Print ( " the this IS test2 " )
 13 is  test1 ()
 14  
15  # higher-order function: internal function calls another function, further comprising a function returns a return value that is a function of memory address name without () 
16  def High_Level_Fun ():
 . 17      test1 ()
 18 is      return test2
 . 19  
20 is  # nested function: def internal function using the function defined in a similar local variable 
21 is  def nest ():
 22 is      def nested ():
 23 is          Print ( " the this IS nested " )
 24- 
25  # were invisible parameter decorative function decorator expand: the execution time of a statistical function of decorator 
26 is  DEF Fun1 ():   # define a function to be decorated 
27      the time.sleep (. 3 )
 28      Print ( " the this Func1 IS " )
 29  
30  # decorator expand written 
31  # decorative implemented 1: definition of a new function, the decorative features as a function of nested function defined in this function, the use of higher-order functions and a decorative function returns the address function 
32  DEF Timer (FUNC):
 33 is      DEF Deco (): # define a calculation parameter corresponding to the function Deco execution time of the function 
34 is          START_TIME = the time.time ()
 35          FUNC ()
 36          stop_time = the time.time ()
37 [          Print ( " % S RUN Time IS% S " % (FUNC, stop_time - START_TIME))
 38 is      return deco # memory address deco function definition statement returns 
39  # decorators achieve 2: Call decorative implemented timer in a defined It will be passed as an argument decorative function, and its return value is assigned to the function needs to be decorated, in this case being decorative function Fun1. 
40 Fun1 = Timer (Fun1) # At this time the memory Fun1 memory pointed Fun1 definition of a function is not the beginning of the data, and this was deco content; 
41  # decorators achieve 3: Fun1 i.e. direct call direct call deco ; a decorative function original definition content when running the new Fun1 is able to find and are executed, the price can see the effect breakpoints 
42 is  Fun1 ()
 43 is  
44 is  # grant 2 and 3 will be omitted when the decorative actually used, in the former function is a function decorated decoration function @ 
45  @timer
 46 is  DEF the Func2 ():
 47     time.sleep(3)
48     print("this is Func2")
49 Func2()

 

Guess you like

Origin www.cnblogs.com/flags-blog/p/11920840.html