python learning 15-- namespace and scope

A. Namespace

Namespace that is stored with the object name mapping / binding local relations. For x = 3, Python will allocate memory space to store the object 3, then the name binding relationship with 3 x stored in
Namespace, del x clears the binding relationship.

1.1 built-in namespace

Accompanied python interpreter startup / shutdown and generation / recovery, so it is the first to be loaded namespace, used to store some built-in names, such as built-in function name
>>> max
<built-in function max> #built-in内建

1.2 global name space

Python file accompanying started / finished produce / recycle, is the second to be loaded namespace, during the execution of the file name generated will be stored in the namespace
, The following names
import sys # sys module name
  x = 1 # variable name x
  if x == 1:
    y = 2 # variable name y
def foo (x): # function name foo
  y = 1
  def bar():
    pass
Class Bar: # class name Bar
  pass

1.3 The local name space

Along with call / end function temporarily generates / recovery, function parameter, defined within the function name will be stored in the namespace
def foo(x):
When y = 3 # call the function, the code will execute the function name x and y are stored in the local name of the function in space
 
Load space is the order of the names:
Built-in namespace -> global namespace -> local name space, and find a name, the name of one of the three must be found from space,
Find the following order:
Local namespace -> global namespace -> built-in namespace.

Two scopes

2.1 global scope and local scope

It can be divided according to different scope names of three names space into two areas:

1. The global scope:

Located global name space, built-in namespace names belong to the global scope, the name of global survival within that range (unless deleted, otherwise the entire text
Member during the execution of survival), the global valid (can be used at any position);

2. Local scope:

Located in the local namespace names belong to the local area. Name within the range of temporary survival (that is temporarily generated when the function is called, after the end of the function call
Is released), the effective local (can only be used within the function).

2.2 Scope and name lookup priority

Find a name in the local scope, the starting position is the local scope, so first find a local name space is not found, go find the global scope: to find the global name
Space is not found, then find the built-in namespace, the end did not find the will throw an exception
def foo(x):
  When y = 3 # call the function, the code will execute the function name x and y are stored in the local name of the function in space
  x = 100 # global scope name x
def foo():
  x = name of the local scope x 300 #
  print (x) # x to find locally
foo () # 300 results
x=100
def foo():
  x = 300 # generate local scope of the function call name x
foo()
print (x) # x looking at the overall situation, the result is 100

2.3 global and nonlocal

 

If you want to change the value of the local (hard type) corresponding to the name of the global need in a global 

x=111  

def func():    

  , Ltd. Free Join x # x statement name is a global name, not the name of the new recycling    

  x=222  

func() 

print(x)   

nonlocal (): the value corresponding to the name (hard type) contained in the outer function modification function 

x=0 

def f1():    

  x=11    

  def f2():        

    nonlocal x        

    x=22    

f2()    

Print ( 'X in F1:', X)   F1 ()

Guess you like

Origin www.cnblogs.com/heirenxilou/p/12524298.html