python 3.7
>>> exec('foobz = 3')
>>> print(foobz)
3
为什么上面的工作,但下面没有
>>>
def blah():
exec('foobz = 3')
print(foobz)
>>> ... ... ...
>>> blah()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in blah
NameError: name 'foobz' is not defined
我已经证实这不是因为间隔,我不清楚区别是什么
这里回答了这个问题,但是tl;dr exec语句在应用程序的范围和生命周期方面遵循不同的规则:
https://stackoverflow.com/a/45535337/6670005
相关问题 更多 >
编程相关推荐