2011-05-08 99 views
1

我已經成功導入了一堆模塊的使用蟒蛇環路檢查變量

from assets import * 

現在我想通過這些導入模塊迴路和檢查特定的變量或函數的一個文件夾中。我嘗試使用dir()函數來獲取導入模塊的列表並查看它們,但是因爲我在循環訪問字符串數組,而不是技術上的模塊數組,所以我無法查找模塊var。

for aModule in dir(assets): 
    if word in aModule.alt: 
     print "found it!" 

if word in aModule.alt:

AttributeError: 'str' object has no attribute 'alt'

+0

我找到了解決辦法在這裏http://loquehumaine.wordpress.com/2011/04/05 /蟒蛇-封裝模塊,如何對環通,全子包/。戈特使用sys.modules [「assets。」+ item] .alt – Chad 2011-05-08 00:23:30

回答

3

我覺得你在做什麼,可以更簡單地完成:

import assets 
for aModule in vars(assets).values(): 
    if hasattr(aModule, 'alt') and word in aModule.alt: 
     print "found it!" 
     print aModule.__name__