繁体   English   中英

通过名称从模块中检索值?

[英]Retrieving Values from Module by Name?

我正在尝试从其他目录中的单独.py文件中读取以下数据,尤其是:“ assets.ovs.screen0”。

这里显示的是assets.ovs.screen0.placement的内容:

selection_Size = [600,110]
selection_Loc  = [1500-600,208]
selection_Mov  = [0,115]

树的底部是一个文件test.py,当前显示为:

import assets.ovs.screen0.placement as placement

requiredVar = "select_"

print placement.select_Size
print eval("placement."+ requiredVar)

假设有更安全/更简便的方法,那会是什么?

我可以这样尝试吗:

param = ["selection_Size", "selection_Loc", "selection_Mov", "error"]
for par in param:
    print par, placement.__dict__.get(par, None)

    # this should work too
    # print par, getattr(placement, par, None)

输出:

selection_Size [600, 110]
selection_Loc [900, 208]
selection_Mov [0, 115]
error None

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM