Variable reference Order
Python refers to the order of variables : The current scope local variable, the outer scope variable, and the global variable->python built-in variables in the current module.
1. Global
The global keyword is used in a function or other local scope, and if global variables are not modified, the Global keyword can be used, and if you want to modify the global variables in a function or local scope, Then the global keyword must be declared in the function or local scope, otherwise the error:unboundlocalerror
Count = 1def foo (): Global Count#如果不加这个那么就会报错count + = 1 print (count) foo ()
2. nonlocal
Usage: closures, nested functions
If an intrinsic function wants to modify a local variable of an external function, it needs to be declared in an intrinsic function:
nonlocal < variable name >
The nonlocal statement searches the definition of the next layer function in the current call stack.
Def a (): Count = 1 def b (): nonlocal count Count + = 1 print (count) return BA () ()
3. Decorative Device
Description: The function of the adorner is to pass the modified function as a parameter to the function corresponding to the adorner, and return the wrapped decorated function. Adorners are actually a special case of closures.
Def A (func): return Func@adef B (): PASSB ()
Perform:
Parsing process:
- If @a is found, then a (b) will be executed and a return B
- B (), this step is actually called the B function of the same name returned by a, and begins to perform the operation in the B function
4. Closed Package
closure concept: in Computer science, closure (Closure) is the abbreviation of the lexical closure, is a function that refers to the free variable, the referenced free variable will exist with this function, even if it has left the environment to create it is no exception.
Brief introduction: Can be understood as a closed package, this package is a function and function inside the corresponding logic, the contents of the package is a free variable, free variables can roam around with the package, such as: The adorner is a closure, with the adorner is passed the function name is a free variable (function), Adorners and internal logic structures are functions and internal logic in closures.
def func (name): Def inner_func (age): Print (' Name: ' +name+ ' Age: ' +age) return Inner_funca = func (' Liyang ') A (a)#> ;>> Name:liyang age:20
In the example above: when invoking the Func function, a closure inner_func is generated, and the free variable held by the closure is name.
When the life cycle of the function Func ends, the name variable still exists because it is referenced by the closure and is not recycled.
5. Delay Binding
The scope of the Python function is determined by the code, which is static, but the use is dynamic and is determined at execution time.
Problems encountered in closures
FS = [Lambda x:x*i for I in range (4)]print (Fs[0] (1))#>>> 3
The expected output is: 0, but the result is: 3
This is because the value of the variable i is searched only when the function foo is executed, and because the loop is over, I points to the final value of 3, so the result will eventually be the same.
Using the following closure method, you will get the desired result:
def foo (i): return Lambad x:x*ifs = [foo (i) for I in range (4)]print (Fs[0] (1))
Reference:
Https://www.cnblogs.com/linxiyue/p/7911916.html
64905070
Python functions and variable scopes