4 回答
TA贡献1876条经验 获得超7个赞
global
global
foo = 1def test(): foo = 2 # new local foodef blub(): global foo foo = 3 # changes the value of the global foo
sub
.
TA贡献1890条经验 获得超9个赞
>>> a = 1>>> def p(): print(a) # accessing global scope, no binding going on>>> def q(): a = 3 # binding a name in local scope - hiding global print(a)>>> def r(): print(a) # fail - a is bound to local scope, but not assigned yet a = 4>>> p()1>>> q()3>>> r()Traceback (most recent call last): File "<pyshell#35>", line 1, in <module> r() File "<pyshell#32>", line 2, in r print(a) # fail - a is bound to local scope, but not assigned yetUnboundLocalError: local variable 'a' referenced before assignment>>>
TA贡献1777条经验 获得超10个赞
value = 42def doit(): print value value = 0doit()print value
value = 0
doit()
print value
UnboundLocalError.
value
value
global
添加回答
举报