(1) Python's LEGB:LEGB refers to the variable lookup according to the order priority of L>E>G>B. l:local function Internal scope, is the lowest level of a single function inside, e:enclosing function inside and within the function, there is an intrinsic function inside the function; G:global global scope is a. py file; b:build-in Scope, such as: tuple,list, tuples. is in all. py files.
(2) ClosuresClosures refer to a function in which another function is embedded, and the inline function uses the parameter variables of the external function as a reference for the different operating modes of the decision-in-line function. The last external function returns its inline function, which returns the address of the function object, and this inline function is what we call a closure. Use the following example to illustrate:
defSet_passline (passline):defCMP (val):ifVal >=Passerine:Print('Pass') Else: Print('failed') returncmpf_100= Set_passline (60) f_100 (89) f_150= Set_passline (96) f_150 (89)Set_passline returns the function object of the inline function CMP, with different arguments passline determining the different CMP function objects, the inline function CMP is the closure. In short, a closure refers to an inline function that references an external function, whose main function is: (1) encapsulation; (2) Code reuse in addition, the external function parameters referenced in the closure can also be functions, when the function is to abstract the same processing parts from several different functions into a closure package, and the closure is passed into the function object. This way, after the same processing part is run in the closure, you can run specific code for different functions. Examples are as follows:
defMy_sum (*Arg):Print('My_sum') returnsum (ARG)defMy_average (*Arg):Print('My_average') returnSUM (ARG)/Len (ARG)defDec (func):#Closed Package defIn_dec (*Arg):Print('in_dec () =', Arg)#encapsulate the same processing department ifLen (arg) = =0:return0 forValinchARG:if notisinstance (val, int):return0#Process the same department, return specific processing of different functions returnFunc (*Arg)returnIn_decmy_sum=Dec (my_sum)Print(My_sum (1, 2, 3, 4, 5, 6))Print(My_sum (1, 2, 3, 4, 5,'6')) My_average=Dec (my_average)Print(My_average (1, 2, 3, 4, 5, 6))Print(My_average (1, 2, 3, 4, 5,'6'))The result of the operation is:
(3) decorative deviceAdorners are essentially the use of closures, by encapsulating code in the same part or some decorative code, for code reuse (somewhat analogous to "decorative patterns" in design patterns). Using the Python interpreter's adorner syntax, the sugar (@dec, Dec is the outer function name of the closure) is used to display the closure, the function as an argument to the closure, and the last adorner to return the closed-decoration function to the decorated function, namely: bar = Dec (bar). Examples of use are as follows:
defDeco (func):Print("This is deco") defIn_deco (x, y):Print('This is closure') func (x, y)returnIn_deco@deco#The syntactic sugar of the adorner, which passes its following function as the Function object argument func into the Deco, equivalent to calling bar = Dec (bar)defBar (x, y):Print("This is bar") Print("%d+%d=%d"% (x,y,x+y)) Bar (2,4)The results of the operation are as follows:
Python Closures and decorators