Function & python- order function & nested function & decorator

Learning Essay:

basic introduction:

Function: function that is variable 
order function: a function name as an argument passed to a parameter
2. Return value function names
nested functions: internal function again defined functions
decorator: decorator is the function, in order to add functionality to other functions
in principle to change the source code decorative function, is installed not modify function is called

high function nested function = + decorator

code is as follows:
1. function
1  # Function 
2  DEF foo (name): # define a function 
. 3      Print ( ' IS the this foo name: ' , name)
 . 4 foo ( ' FOO ' )    # function foo call 
. 5 foo2 = foo   # function i.e. variable, foo as an argument passed foo2 
. 6 foo2 ( ' foo2 ' ) # foo2 call, i.e. modulation foo

2. Higher-order functions

1  # higher order function 
2  
. 3  DEF Test (func):
 . 4      START_TIME = the time.time ()
 . 5      func ()
 . 6      END_TIME = the time.time ()
 . 7      Print ( ' function func running time: ' , STR (start_time- END_TIME ))
 . 8      return FUNC
 . 9  DEF Demo ():
 10      Print ( " demo_% S runs \ n 3 seconds sleep " )
 . 11      the time.sleep (. 3 )
 12 is      Print ( " demo_% S end of run " )
13 is Test (Demo)    # passed to the function argument, and executes 
14 Demo Test = (Demo)   # Test (Demo) function returns an argument to Demo 
15 Demo () # Demo performed

3. nested functions

1  # nested function 
2  DEF DOC ():
 . 3      name = ' Loren ' 
. 4      DEF doc1 ():
 . 5          name = ' loren1 ' 
. 6          DEF doc2 ():
 . 7              name = ' loren2 ' 
. 8              Print ( ' name: ' , name )
 . 9          doc2 ()
 10      doc1 ()
 . 11 DOC ()

4. decorator

1  # decorator 1 
2  
. 3  DEF Test (FUNC):
 . 4      DEF warppr (* args, ** kwargs):
 . 5          Print ( ' can add a new function & features ' )
 . 6          Print ( ' performs the primitive ' )
 . 7          FUNC (* args, ** kwargs)
 . 8      return warppr
 . 9  DEF Demo (name):
 10      Print ( ' demo_% S end of run ' % name)
 . 11  
12 is Demo ( ' Loren ' )  # Call the original function of the demo 
13 is demo = Test (demo) # decorative function demo 
14 demo ( ' loren2 ' )   # rear trim calling function a demo

Built-python decorator: @

1  # decorator 2 
2  DEF Test (FUNC):
 . 3      DEF warppr (* args, ** kwargs):
 . 4          Print ( ' can add a new function & features ' )
 . 5          Print ( ' performs the primitive ' )
 . 6          FUNC (* args , ** kwargs)
 . 7      return warppr   # return address of the function warppr 
. 8 @Test   # @Test equivalent = Test Demo (Demo) 
. 9  DEF Demo (name):
 10      Print ( ' demo_% S end of run ' % name)
. 11  
12 is demo ( ' Loren ' ) # rear trim function call demo

 

 

Guess you like

Origin www.cnblogs.com/lorenjia/p/11100201.html