[TOC]
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 the namespace, del x clears the binding relationship.
There will be space for up to three names during program execution
Illustration: spoof 19
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内建
Illustration: spoof 20
1.2 global name space
Beginning with the execution of python files / 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
x=1 #变量名x
if x == 1:
y=2 #变量名y
def foo(x): #函数名foo
y=1
def bar():
pass
Class Bar: #类名Bar
pass
Illustration: spoof 21
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):
y=3 #调用函数时,才会执行函数代码,名字x和y都存放于该函数的局部名称空间中
Namespace load order are: built-in namespace -> global namespace -> local name space, and find a name, the name of one of the three must be found from space, look for the following order: Local namespace -> global namespace -> built-in namespace.
Illustration: spoof 22
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:
-
Global scope: Located global name space, built-in namespace names belong to the global scope, the name of global survival within that range (unless removed, otherwise survive during the execution of the entire document), the global valid (can be in any position use);
- The 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 release), locally effective (can only be used within the function).
Illustration: spoof 23
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 to the global scope is searched first: Find a global name space, not found, then find the built-in namespace, finally We have not found it will throw an exception
x=100 #全局作用域的名字x
def foo():
x=300 #局部作用域的名字x
print(x) #在局部找x
foo()#结果为300
Find a name in the global scope, the starting position is the global scope, so to find a global name space, not found, then find the built-in namespace, the end did not find the will throw an exception
x=100
def foo():
x=300 #在函数调用时产生局部作用域的名字x
foo()
print(x) #在全局找x,结果为100
Tip: You can call built-in functions locals () and globals () to see the name of each local scope and global scope to view the result is the dictionary format. The results in the global scope view of locals () is equal to the globals ()
Illustration: spoof 24
Python supports nested-defined functions, the lookup function embedded in the name, the name will give priority to look for its own local scope, and then look for external nested function definitions of the scope of the layers from the inside out, is not found, Find global scope
x=1
def outer():
x=2
def inner(): # 函数名inner属于outer这一层作用域的名字
x=3
print('inner x:%s' %x)
inner()
print('outer x:%s' %x)
outer()
#结果为
inner x:3
outer x:2
Inside the function, no matter how many layers of nested, you can view the name of the global scope, to modify the value of the global namespace name within a function, when the value of an immutable type, you need to use the global keyword
x=1
def foo():
global x #声明x为全局名称空间的名字
x=2
foo()
print(x) #结果为2
When the value of the variable type arguments, modification of the value of the function to the initial value directly vivo reaction,
num_list=[1,2,3]
def foo(nums):
nums.append(5)
foo(num_list)
print(num_list)
#结果为
[1, 2, 3, 5]
For functions nested multi-layered, using nonlocal keyword can be declared as a name from the scope of the external nested function definitions (non-global)
def f1():
x=2
def f2():
nonlocal x
x=3
f2() #调用f2(),修改f1作用域中名字x的值
print(x) #在f1作用域查看x
f1()
# 结果
3
nonlocal x function will start from the outer layers of the current function to find the name x, if all the way to the outermost function can not be found, an exception is thrown.
Illustration: spoof 25