In Python, variable lookup follows the LGB principle of prioritizing a variable in a local scope (regional scope), failing to find it in the global scope, and finally trying to find it in the built-in scope (build-in scope). If it is still not found, an exception is thrown. Later, due to the appearance of closures and nested functions, the scope adds an external scope, so that the lookup scope priority of the variable becomes: local, external, global, and built-in. Scopes are produced by statements such as Def, class, and lambda, and if, try, and for statements do not produce new scopes. Variable name references are divided into three scopes to find: first, locally, then within the function (if any), after the global, and finally the built-in. By default, a variable name assignment creates or alters a local variable. The global declaration assigns a value to the variable name that maps to the scope within the module file. Python's variable name resolution mechanism is also known as the LEGB Law, as follows: When you use an indeterminate variable name in a function, Python searches for 4 scopes: The local scope (L), then the local scope (E) of def or lambda in the previous layer of nesting, followed by the global scope (G) , and finally a built-in scope (B). Follow this search principle and stop where you found the first place. If it's not found, Python will complain.
A = 1
def f ():
a = 2
def g ():
print a//[1]: output is 2 return
g
func = f ()
func ()//[2]
The function called at [2] in the code actually calls the inline function g defined in function f, and at [1] of the code, the output of "print a" in function g is 2. At first glance there is some doubt, because the constraint "a = 2" within the function f should not function outside of it, and when executing func (), the constraint should be "a = 1". But as we said before, the scope is only determined by text, and function g is within the function f, so the scope of function g definition is embedded within the scope of function F. In other words, the scope of function f is the direct peripheral scope of the scope of function g, so the first name reference at [1] should refer to the constraint created in the scope defined by function f, according to the most nested scope rule.
Although at [2] in Listing 8-2, the constraint "a = 2" is no longer working, but when Python executes "func = f ()", it executes the "Def g ()" statement in function f, where Python binds the constraint "a = 2" to the function object corresponding to function g, Returns the bundle of results, the bundle of which is called a "closure."
In fact, there is a rather delicate question of whether the most nested scope rule is the result of a "closure", or "closure" is the implementation of the most embedded set of scoping rules. The two issues appear to be consistent, but they imply who determines who's relationship. In fact, Python implements closures in order to implement the most nested scope rules. In other words, the most nested scope rule is the design strategy of language design, that is, the metaphysical "Tao", and the closure is a solution to the language, that is, the form of the "Device".
The code snippet that Python can change the scope of a variable is Def, class, Lamda.
Def scopetest (): localvar=6; Print (Localvar) scopetest () #print (Localvar) #去除注释这里会报错 because Localvar is a local variable if/elif/else, TRY/EXCEP
T/finally, for/while while true:newvar=8 print (Newvar) break; Print (Newvar) try:newlocal=7 raise Exception except:print (newlocal) #可以直
Oh, yes. Output: 8 8 7 Visible This keyword defines variables, their scopes are consistent with the outside, and this is a bit different from the Java scope concept.
The variable search path is: local variable-> global variable def scopetest (): var=6; Print (VAR) # var=5 Print (Var) scopetest () print (VAR) output result: 5 6 5 here Var first searches for local variables, SC The var=6 in Opetest () is equivalent to defining a local variable, which is assigned a value of 6.
Of course, if you do want to modify the value of a global variable, you need the following: Def scopetest (): Global var var=6; Print (VAR) # var=5 Print (Var) scopetest () print (VAR) output result: 5 6 6 look at one more of this: def
Scopetest (): var=6; Print (Var) # def Innerfunc (): Print (Var) #look here Innerfunc () var=5
Print (Var) scopetest () print (VAR) output results: 5 6 6 5