This article mainly introduces how python uses the dot operator to access the dictionary (dict) data, and involves the Python dictionary operation skills, for more information about how to use the dot operator to access Dictionary (dict) data in python, see the following example. Share it with you for your reference. The specific analysis is as follows:
Generally, the access to the dictionary is similar to: dict ['name'], if you can use dict. the following code defines a class to provide this method.
Class 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. allowdoprint (state = False) print (d ['foo']) # bar from http://www.jb51.netprint (d. foo) # AttributeError: 'dottabledict 'object has no attribute 'foo'
I hope this article will help you with Python programming.