简体   繁体   中英

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".

Show here is the contents of 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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