Python3 and python3
1 ''' 2 knowledge points used: 3 1. scope: L> E> G> B 4 2. upper-order function: 5. If one of the two conditions is met, the following can be used: 6 1. function name can be input as a parameter. the function name can be used as the return value. closure 9 10''' 11 12 13 ''' 14 closure 15 the closure in python is defined as: 16 in the form of an internal function, when variables in the external scope (but not in the global scope) are referenced, 17 internal functions are called closures. 18 ''' 19 # def out1 (): 20 # x = 10 21 # def in1 (): # internal function 22 # y = 1 23 # print (x) # reference variables in the external scope 24 # return in1 # The internal function in1 is called the closure 25 #26 # Call in1 () 27 # Method 28 # out1 ()() 29 ## method 2 30 # p = out1 () 31 # p () 32 ## 33 ## in1 () # local variables, globally, the 34 #35 #36 ''' 37 closure = function block (internal function) + environment when defining the function (variable reference) cannot be called) 38 '''39 #40 # import time 41 #42 ## start = time. time () # Start time 43 # time. sleep (1) # intermittent time, intermittent 1 s 44 ## end = time. time () # End time 45 #46 # def p1 (): 47 ## print (start) # calculate the number of seconds 48 # st1 = time. time () 49 # print ('Hello World') 50 # time. sleep (5) 51 # end1 = time. time () 52 # print ('% s seconds consumed' % (end1-st1) 53 # p1 () 54 # 55''' 56 ...... 57 n multiple functions similar to those that require time calculation 58 at this time, for convenience, create a function 59''' 60 #61 # def showtime (x) dedicated for time calculation ): 62 # start2 = time. time () 63 # x () 64 # end2 = time. time () 65 # print ('Program spent % s second' % (end2-start2) 66 #67 # def test (): 68 # time. sleep (2) 69 # print ('yeah') 70 #71 # showtime (test) 72 #73 ''' 74 decorator: 75 Add new function 76''' 77 #78 #79 # def showt (c): 80 # def a (): 81 # start2 = time. time () 82 # c () 83 # end2 = ti Me. time () 84 # print ('Program spent % s second' % (end2-start2 )) 85 # return a 86 #87 # @ showt # equivalent to z = showt (test1) 88 # but before the function 89 # def test1 (): 90 # time. sleep (1) 91 # print (123) 92 # test1 () 93 #94 #95 ''' 96 parameter modifier 97 ''' 98 #99 # def fun1 (x): 100 # def in_fun1 (* y, ** z): 101 # start = time. time () 102 # x (* y, ** z) 103 # end = time. time () 104 # print ('this program uses % s second' % (end-start) 105 # return in_fun1106 #1 07 # @ fun1108 #109 # def fun2 (* a, ** B): 110 # Sum = 0111 # for I in a: 112 # Sum + = i113 # print (Sum) 114 # time. sleep (2) 115 # fun2 (3, 5, 1) 116 #117 #118 # function addition parameter 119 # def loge (s = ''): 120 # def fun3 (q): 121 # def infun3 (* x, ** y): 122 # start = time. time () 123 # q (* x, ** y) 124 # end = time. time () 125 # print ('% s second' % (end-start) 126 # if s = 'true': 127 # print ('loge !!! ') 128 # return infun3129 # return fun3130 #131 # @ loge ('true') # The intermediate parameter 'true' can be written as another one, except that loge132 # def test3 (): 133 # print ('test3') 134 # time. sleep (1) 135 # test3 ()