【发布时间】:2019-09-05 11:23:47
【问题描述】:
我有字符串格式的 python 函数,我想在程序范围内获取这些函数的 python 对象。我尝试过exec()、eval() 和ast.literal_eval(),但这些都没有返回函数对象。
例如:
s = "def add(args):\n try:\n return sum([int(x) for x in args])\n except Exception as e:\n return 'error'\n
这是一个简单的字符串函数,用于添加列表元素。我正在寻找一个实用模块,它可以返回函数add的对象
function_obj = some_function(s)
print 'type:', type(function_obj)
type: <type 'function'>
【问题讨论】:
-
为什么你有字符串格式的函数定义?
标签: python python-2.7