This article illustrates how Python uses the dot operator to access dictionary (dict) data. Share to everyone for your reference. The specific analysis is as follows:
The usual access dictionary uses something like: dict[' name ', and if it's easier to access through Dict.name, the following code customizes 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 '])
# bar
Print (D.foo)
# bar
d.allowdotting (state=false)
print (d[' foo '])
# Bar from Http://www.jb51.net
print (D.foo)
# attributeerror: ' Dottabledict ' object has no attribute ' foo '
I hope this article will help you with your Python programming.