类和函数是不同的,类内部的变量实际上是作为其属性分配给类的名称空间的,而在函数内部,变量只是不能在其外部访问的普通变量。
实际上,函数内部的局部变量是在首次解析该函数??时确定的,并且python不会在全局范围内搜索它们,因为它知道您已将其声明为局部变量。
因此,一旦python看到x = x + 1
(赋值)并且没有global
为该变量声明,那么python将不会在全局或其他范围内寻找该变量。
>>> x = 'outer'
>>> def func():
... x = 'inner' #x is a local variable Now
... print x
...
>>> func()
inner
>>> x = 'outer'
>>> def func():
... print x #this won't access the global `x`
... x = 'inner' #`x` is a local variable
... print x
...
>>> func()
...
UnboundLocalError: local variable 'x' referenced before assignment
但是,当您使用一条global
语句时,则使用python在global
范围内查找该变量。
阅读:当变量具有值时,为什么会收到UnboundLocalError?
:对于嵌套函数,您可以使用nonlocal
py3.x中的语句修改在封闭函数中声明的变量。
但是类的工作方式不同,实际上x
在类内部声明的变量A
变为A.x
:
>>> x = 'outer'
>>> class A:
... x += 'inside' #use the value of global `x` to create a new attribute `A.x`
... print x #prints `A.x`
...
outerinside
>>> print x
outer
您还可以直接从全局范围访问类属性:
>>> A.x
'outerinside'
global
在课堂上使用:
>>> x = 'outer'
>>> class A:
... global x
... x += 'inner' #Now x is not a class attribute, you just modified the global x
... print x
...
outerinner
>>> x
'outerinner'
>>> A.x
AttributeError: class A has no attribute 'x'
>>> x = 'outer'
>>> class A:
... print x #fetch from globals or builitns
... x = 'I am a class attribute' #declare a class attribute
... print x #print class attribute, i.e `A.x`
...
outer
I am a class attribute
>>> x
'outer'
>>> A.x
'I am a class attribute'
规则:如果没有global
和nonlocal
被使用,则蟒蛇搜索顺序。
>>> outer = 'global'
>>> def func():
enclosing = 'enclosing'
def inner():
inner = 'inner'
print inner #fetch from (L)ocal scope
print enclosing #fetch from (E)nclosing scope
print outer #fetch from (G)lobal scope
print any #fetch from (B)uilt-ins
inner()
...
>>> func()
inner
enclosing
global
<built-in function any>