Python3:如何使用exec?

时间:2019-02-11 11:56:33

标签: python python-3.x python-exec

如何在用户功能中使用exec? 为什么功能未定义?

def func():
    exec('f = lambda x: x + 2')
    print(f(100))

if __name__ == "__main__":
    func()

Traceback (most recent call last):
  File "./playground/playground.py", line 10, in <module>
    func()
  File "./playground/playground.py", line 7, in func
    print(f(100))
NameError: name 'f' is not defined

0 个答案:

没有答案