>>> class Config(object):
... def __init__(self, **kw):
... self.__dict__.update(kw)
...
那么:
>>> cfg = Config(host = '127.0.0.1', port = '8080')
>>> print "%s:%s"%(cfg.host, cfg.port)
127.0.0.1:8080
更变态版本:
>>> def Config(**kw):
... obj = type('Config', (), kw)()
... return obj
...
>>> c = Config(host = '127.0.0.1', port = '8080')
>>> c.host, c.port
('127.0.0.1', '8080')
来自: http://blog.csdn.net/lanphaday/archive/2008/09/09/2905877.aspx
andelf fledna Feather
2008年10月2日星期四
强悍的类定义
订阅:
博文评论 (Atom)

没有评论:
发表评论