簡體   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