Nesting: function defined functions within the
closure: Open Closed Principle meet: add new features to function without modifying the source code is called the
# Global global variables into local variables NUM = 100 DEF Fn1 (): Global NUM NUM = 600 return NUM Print (Fn1 ()) # 600 # nonlocal nested variables into local variables DEF Outer (): NUM = 888 def : Inner () nonlocal NUM NUM = 666 Print (NUM) # 666 Inner () Print (NUM) # 666 Outer () # decorator # to add new features without changing the source code under the circumstances def Outer (FUNC): DEF Inner (* args, ** kwargs): # pass # new features RES = FUNC (* args, ** kwargs) # decompression bring original function Pass # new function return RES return Inner # decorator (outer), to give a new function (Inner) @outer DEF Fn (): Pass RES = Fn () Print (RES) DEF foo (Fn): # define a nested function DEF bar (a): Fn (a * ( A -. 1 )) Print ( " * " * 15,) Return Fn (A * (A -. 1 )) return bar '' ' below the decorative effect is equivalent to: foo (my_test), my_test will replace (decorative) to return the value of the statement; Since foo () function returns the bar function, so my_test bar is at the same time, a parameter corresponding to the bar my_test function parameters a '' ' @foo DEF my_test (a): Print ( " == my_test function == " , a) # print my_test function, see It is actually a bar function Print (my_test, 1111111 ) # The following code looks calling my_test (), in fact, is to call bar () function my_test (10)