【发布时间】:2012-04-13 10:09:46
【问题描述】:
# coding: utf-8
def func():
print 'x is', x
#x = 2 #if I add this line, there will be an error, why?
print 'Changed local x to', x
x = 50
func()
print 'Value of x is', x
- 我没有在func函数中添加
global x,但仍然可以找到x是50,为什么? - 我在func函数中添加
x=2行时,会报错(UnboundLocalError: local variable 'x' referenced before assignment),为什么?
【问题讨论】:
-
@Marcin 如果我添加 x = 2,错误是“UnboundLocalError: local variable 'x' referenced before assignment”
-
然后把它放在你的问题中。
标签: python