在python程序中解释python代码
是否有用于在 python 程序中解释 python 代码的库?
示例用法可能如下所示..
code = """
def hello():
return 'hello'
hello()
"""
output = Interpreter.run(code)
print(output)
然后输出
hello
回答
从grepper找到这个例子
the_code = '''
a = 1
b = 2
return_me = a + b
'''
loc = {}
exec(the_code, globals(), loc)
return_workaround = loc['return_me']
print(return_workaround)
显然你可以将全局和局部范围传递到exec. 在您的用例中,您只需使用命名变量而不是返回。