为什么以下python3代码会产生错误?
a='''
def x():
print(42)
'''
class Test:
def __init__(self):
exec(a)
x()
t = Test()
Run Code Online (Sandbox Code Playgroud)
结果在此消息中:
Traceback (most recent call last):
File "bug.py", line 11, in <module>
t = Test()
File "bug.py", line 9, in __init__
x()
NameError: global name 'x' is not defined
Run Code Online (Sandbox Code Playgroud)