9.23 在局部变量域中执行代码
问题
解决方案
>>> a = 13
>>> exec('b = a + 1')
>>> print(b)
14>>> def test():
... a = 13
... exec('b = a + 1')
... print(b)
...
>>> test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in test
NameError: global name 'b' is not defined讨论
Last updated