Scope, closures, global, nonlocal

1. Scope basis

  • python in all variable names related events have occurred in the assignment, the variable name for the first time when there is an assignment, and to use the variable must be assigned. Since python is no variable declarations (such as java, c #), the region in variable assignment determines the namespace of the variable.
  • Variables declared in a function (DEF declarations), the only access within the function, and can be a function of the same variable declarations outside, do not affect each other, but can be accessed through a global variable or a function outside the nonlocal but two there are differences in the scope and use of keywords.
  • Variable name resolution principles: LEGB rules

      When an undefined variable name in the function, will be from 4 to find scope, first local scope (L), if there is a function nested function, the scope intrinsic function looks for an external function ( E), to find the global scope (G), and finally built scope, referring to a built-in python scope defined within some keywords stored in builtins module (B) in the python3.X.

2.global

  • global statement tells the python function program generates one or more global variable names ----- short answer is that you define a variable domain global scope inside the function can access the variable (function is not the same variable name), To modify the variables first with a global keyword to declare within a function.
  • In a global variable, even useless to declare a variable, but can also be used to specify a variable as a global variable by global keyword in the function.
= 99 x
 DEF F (): 
    x + =. 1   # being given, is equivalent to x = x + 1, in the function assignment without first x 
    Print (x) 
F ()

Improve:

99 = X
 DEF F ():
     Global X # key 
    X + =. 1 Print (X) # 100 F ()
 Print (X) # 100
    
View Code
Y, Z = 1,2
 DEF F ():
     Global x # key 
    x Y + Z =     # x to define a global variable, 
    Print (x) # 3 
F ()
 Print (x) # external access function 3
View Code

3. Closure

Closure present in the nested functions, in fact, the closure means of the extended function scope, which contains a reference to the function definition body.

DEF F1 (): 
    x =. 4 
    Action = ( the lambda n-: ** n-x) # anonymous function, external function call after the value of x is stored 
    return Action    # return function object 

AC = F1 ()
 Print (AC (. 3)) # 64
View Code

In the above lambda, x is a free variable, refers to the variable domain unbound local role in the function __closure__ binding properties can be bound to return values ​​by

DEF F1 (): 
    x =. 4 
    Action = ( the lambda n-: ** n-x) # anonymous function, external function call after the value of x is stored 
    return Action    # return function object 

AC = F1 ()
 Print (AC (. 3)) # 64 
Print (AC. The __code__ .co_freevars)   # ( 'X',) 
Print (AC. __closure__ [0] .cell_contents)    # . 4
View Code

Closure is a function that retains the binding free variables exist in defining a function, when this function is called after the external function call though its scope is not available, but you can still use those values ​​binding.

4.nonlocal

The statement and global keyword to declare similar, but the keywords can only function in the nested function, and when the variable is declared external function must first define the variables (with different global, global can not first defined in the global variable) .

DEF Tester (Start): 
    State = Start
     DEF nested (label): 
        nonlocal State    # are declared with the keyword, in fact, there are closures seen above, even if the statement can not be accessed, but can not modify this value, but in a statement external functions must define the variable scope 
        Print (label, State) 
        State + =. 1
     return nested 

N1 = Tester (. 1 ) 
N1 ( ' GTR ' )      # GTR. 1 
N1 ( ' gtr111 ' )   # gtr111 2 

N2 = Tester ( 10 ) 
N2 ( ' VFV ' )     # VFV 10
n2 ( ' gtrg ' )    # gtrg 11 
# which you can see n1 n2 objects passed parameters (1, 10) independently independently of each other, in essence, run once every tester function creates a new variable domain
View Code

 

Guess you like

Origin www.cnblogs.com/2019zjp/p/11868617.html