【发布时间】:2016-03-28 15:17:37
【问题描述】:
虽然我将该结构纳入模块范围:
def test():
return 1
test = test()
print test
效果很好。
但如果我在函数范围内尝试相同:
def test():
return 1
def go():
test = test()
print test
我收到UnboundLocalError:
Traceback (most recent call last):
File "my.py", line 16, in <module>
go()
File "my.py", line 12, in go
test = test()
UnboundLocalError: local variable 'test' referenced before assignment
我有点困惑。为什么这些行为之间会有这样的差异?
【问题讨论】:
标签: python python-2.7 scope namespaces