简体   繁体   English

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

[英]Retrieving Values from Module by Name?

I'm attempting to read the following data from a separate .py file in a different directory, specifically: "assets.ovs.screen0". 我正在尝试从其他目录中的单独.py文件中读取以下数据,尤其是:“ assets.ovs.screen0”。

Show here is the contents of assets.ovs.screen0.placement: 这里显示的是assets.ovs.screen0.placement的内容:

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

At the base of the tree is a file, test.py, currently reading: 树的底部是一个文件test.py,当前显示为:

import assets.ovs.screen0.placement as placement

requiredVar = "select_"

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

Assuming there is a safer/easier way of doing this, what would it be? 假设有更安全/更简便的方法,那会是什么?

Can i try this way: 我可以这样尝试吗:

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)

Output: 输出:

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