Python- Scope

The visible range of an identifier is the scope identifier. General often said that the scope of the variable.

Look at the piece of code

# -*- coding:utf-8 -*-
x=5
def foo():
    print(x)
foo()

  

1 x=5
2 def foo():
3     x+=1
4     print(x)
5 foo()

Q: x visible in the end is not visible?

 

Here on the introduction of two concepts

Global scope --global

Visible throughout the program runtime environment.

Local scope - local

Visible internal functions, and the like. Local variables can not exceed the scope of its use in the local scope.

 

Back to the above function

X = 5 in the first code is defined, the global scope is quite visible in the external function throughout the program execution environment. Therefore, paragraph 1 of the program is no problem.

Paragraph 2 of the code will be reported unassigned local variable is referenced. Why? That is the definition of assignment, x + = 1 is equivalent in function redefines a local variable x, then foo inside all the local variable X is X, but X did not finish the assignment was used to do the right side of the operation plus 1 (assignment statement executes the right of the equal sign).

 

Nested functions

eg1

 1 # -*- coding:utf-8 -*-
 2 def outer1():
 3     o=65
 4     def inner():
 5         print("inner {}".format(o))
 6         print(chr(o))
 7     print("outer {}".format(o))
 8     inner()
 9 
10 outer1()

Output:

outer 65
inner 65
A

 

EG2

 1 # -*- coding:utf-8 -*-
 2 def outer2():
 3     o=65
 4     def inner():
 5         o=67
 6         print("inner {}".format(o))
 7         print(chr(o))
 8     print("outer {}".format(o))
 9     inner()
10 
11 outer2()

Output:

outer 65
inner 67
C

As can be seen from the example nested structure:

Variable Scope outer visible inner scope;

Inner innermost scope, if defined o = 67, corresponding to the current scope redefine a new variable o, but this does not cover the outer scope o the outer o.

 

Global variables Global

1 # -*- coding:utf-8 -*-
2 x=5
3 def foo():
4     global x  
5     x+=1

Use the keyword global variables, x statement within the definition of foo is all scopes use external X. X must be defined in the global scope.

 

1 # -*- coding:utf-8 -*-
2 def foo():
3     global x
4     x=20
5     x+=1

Use the keyword global variables, x statement within the definition of foo is all scopes use external X.

However, i.e. X = 20 assignments defined above, x is within the scope of action of an external variable assignment, all x + = 1 without error. Note, the scope or the global x Here are.

 

Principles for the use of the Global

  • External scope variables within the scope visible, but do not directly use the local scope inside this (because the objective function is to package, isolated from the outside as much as possible);
  • If the function requires the use of an external global variables, using the transfer function parameter arguments solve;
  • A word not global.

Closure

Free variables: local scope variables is not scheduled. Such as those defined in the variable effect of the outer layer of the inner function of the function domain.

Closure: is a concept appears in the nested function. Refers to a function reference to the inner layer consisting of variable function closure is formed.

 1 # -*- coding:utf-8 -*-
 2 def couter():
 3     c=[0]
 4     def inc():
 5         c[0]+=1
 6         return c[0]
 7     return inc
 8 foo=couter()
 9 print(foo(),foo())
10 c=200
11 print(foo())

Output:

1 2
3

Line 5 is a modification of the elements of variables, rather than on variable C assignment.

c = 200 and c is not in the same couter, inc is a free variable referenced in the couter c.

 

Guess you like

Origin www.cnblogs.com/dongliping/p/11588218.html