[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x=111
>>> def func():
... x=222
... print(x)
...
>>> func()
222
>>> def func1():
... print(x)
... x=333
...
>>> func1()
Traceback (most recent call last):
File "
File "
UnboundLocalError: local variable 'x' referenced before assignment
>>> def func2():
... print(x)
...
>>> func2()
111
>>>
判断变量是全局变量还是局部变量,以定义阶段为准,func2没有报错是因为将变量识别为全局变量,func1报错是在运行阶段时的报错,是因为将变量识别为局部变量