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'         

评论关闭