python使用点操作符访问字典(dict)数据,pythondict,#from http:/
文章由Byrx.net分享于2019-03-23 07:03:16
python使用点操作符访问字典(dict)数据,pythondict,#from http:/
#from http://www.byrx.netclass DottableDict(dict): def __init__(self, *args, **kwargs): dict.__init__(self, *args, **kwargs) self.__dict__ = self def allowDotting(self, state=True): if state: self.__dict__ = self else: self.__dict__ = dict()d = DottableDict()d.allowDotting()d.foo = 'bar'print(d['foo'])# barprint(d.foo)# bard.allowDotting(state=False)print(d['foo'])# bar from http://www.byrx.netprint(d.foo)# AttributeError: 'DottableDict' object has no attribute 'foo'
评论关闭