【发布时间】:2016-12-30 23:43:27
【问题描述】:
如何通过 exec 将参数传递给代码对象并在 python 中打印?下面只是几个例子,看看事情在一般情况下是如何工作的。
def foo(x, y):
return x * y
exec(foo.func_code {'x': 1, 'y': 5}) # This didn't work
def bar():
return 3*5
exec(bar.func_code) # this got executed but I couldn't print it?
【问题讨论】:
-
exec('print(foo(1,5))') -
你为什么要这样做?为什么你需要准确地使用
exec?我觉得有更好的方法来完成你想做的任何事情。
标签: python