python settings using dir getattr setattr

dir()函数

内建dir()列出模块定义的标识符(函数,类或者变量)

getattr(object,name,default)
取得object的name成员的value,如果name成员不存在就返回default的值,如果default未给出则raise AttributeError.

setattr(object,name,value)
新建/修改object的name成员的值为value。

Example:
#settings.py
BEHAVIORS={
  'Red':'generic',
  'Blue':'post',
  'Gold':'sharing',
  'Green':'upload',
   'Orange':'tag',
   'Purple':'downld',
}



#Test.py
import setings as _settings

class Settings():
     def __init__(self,setting_module):
          for setting ii dir(setting_module):
               if setting == setting.upper():
                    setattr(self.setting,getattr(setting_module,setting))
settings = Settings(_settings)

try:
     print getattr( settings, 'BEHAVIORS')
except AttributeError,e:
     print e




你可能感兴趣的:(python,dir,getattr,setattr)