为什么python的exec似乎不能在函数中工作?

2024-03-29 01:11:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我已经证实这不是因为间隔,我不清楚区别是什么


Tags: inmostdefstdinlinecallfileexec