# 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
),为什么?