【发布时间】:2015-10-31 11:38:14
【问题描述】:
我有一个外部模块,我想将“exec”表达式设置为全局(因为我需要将字符串作为变量名传递)
所以,我有一个类似的功能
def fct_in_extern_module():
exec 'A = 42' in globals()
return None
在我的主脚本中,我有
import extern_module
extern_module.fct_in_extern_module()
那么,我为什么会有
NameError: name 'A' is not defined
如果我这样做(在主脚本中)
def fct_in_main():
exec 'B = 42' in globals()
fct_in_main()
>>> B
42
知道如何在外部模块中将字符串“A”设置为变量名吗?
谢谢!
【问题讨论】:
-
你试过
extern_module.A吗?不确定,但这可能是问题所在。
标签: python string exec global python-module