3
我遇到了幾個相關的答案,但不是我想要的。來自字符串的Python動態函數
這裏是我現在的代碼:
code_str = """
print "this is my global x = " + x
print "And another line is done"
"""
x = 'mystery'
func_template = lambda p: None
func_template.__code__ = compile(code_str, '<string>', 'exec')
func_template() # this executes fine and also has access to x, which i do need.
# func_template('param') # This raises: TypeError: <module>() takes no arguments (1 given)
一些背景知識;該code_str會從數據庫中來了,我需要存儲大量的功能在一個字典,所以我可以調用名稱的任何一個,象下面這樣:
all_funcs = {}
# Assuming db_result returns a list of name, code tuples from the databse
for name, code in db_result:
all_funcs[name] = my_compile(code)
我想接着剛纔打電話與所需的功能論據我想,如果我知道這個名字:
result = all_funcs[by_name](arg1, arg2)
編輯:數據庫是可信的,所以我並不需要santize或擔心惡意代碼。