Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the _ _ dict__ attribute in python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use the _ _ dict__ attribute in python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the _ _ dict__ attribute in python".

Tips for converting dictionaries to objects:

Bokeyuan = {"b": 1, "o": 2, "k": 3, "e": 4, "y": 5, "u": 6, "a": 7, "n": 8,} class Dict2Obj: # def _ init__ (self) Bokeyuan): # self.b = bokeyuan ['b'] # self.o = bokeyuan ['o'] # self.k = bokeyuan ['k'] # self.e = bokeyuan ['e'] # self.y = bokeyuan ['y'] # self.u = bokeyuan ['u'] # self.a = bokeyuan ['a'] # self.n = bokeyuan [' N'] def _ _ init__ (self Bokeyuan): print ("before modification:", self.__dict__) self.__dict__.update (bokeyuan) print ("after modification:", self.__dict__) d = Dict2Obj (bokeyuan) print (d) D:\ MC\ venv\ Scripts\ python.exe D:/MC/test03.py before modification: {} after modification: {'baked: 1,' oval: 2, 'kink: 3,' eBay: 4 'yearly: 5, 'upright: 6,' averse: 7, 'nasty: 8} Process finished with exit code 0

The instance of Python has its own _ _ dict__ attribute, and its corresponding class also has its own _ _ dict__ (but some special objects do not have a _ _ dict__ attribute, which will not be discussed here), _ _ dict__ is a dictionary, the key is the property name, and the value is the attribute value.

The properties of the above self instance object are first {}, and then changed to {'baked: 1,' oval: 2, 'kink: 3,' eBay: 4, 'yearly: 5,' upright: 6, 'averse: 7,' nicked: 8} after update

# first create a class cls that contains a class variable clsvar with a value of 1 An instance variable insvar with a value of 2 Class cls: clsvar= 1 def _ init__ (self): self.insvar = 2 # create an instance of the class ins1 and ins2ins1 = cls () ins2 = cls () print (cls.__dict__) # _ _ dict__ attribute print (ins1.__dict__) # instance object ins1 _ _ dict__ attribute ins1.clsvar=20print (cls.clsvar) print (ins1.clsvar) print (ins2.clsvar) # ins1 instance change Changing the class variable does not affect the _ _ dict__ attribute of the ins2 instance print (cls.__dict__) # class print (ins1.__dict__) # the _ _ dict__ attribute of the instance object ins1 D:\ MC\ venv\ Scripts\ python.exe D:/MC/test03.py {'_ module__':'_ _ main__' 'clsvar': 1,' _ _ init__':,'_ _ dict__':,'_ _ weakref__':,'_ _ doc__': None} {'insvar': 2} 1201 {' _ module__':'_ _ main__', 'clsvar': 1,' _ _ init__':,'_ _ dict__':,'_ _ weakref__': '_ _ doc__': None} {' insvar': 2, 'clsvar': 20} Process finished with exit code 0

When printing the _ _ dict__ attribute of a class, it lists the attributes contained in the class cls, including some of the class built-in properties and class variable clsvar, as well as the constructor _ _ init__

The instance variable is contained in the _ _ dict__ attribute of the instance object ins1. The property search order of an object follows the order of finding the instance object itself, then the class, and then the parent class of the class. When ins1 finds clsvar in its _ _ dict__, it will no longer look up

The sentence ins1.clsvar = 20 only adds the key-value pair of 'clsvar': 20' to the _ _ dict__ attribute of the instance ins1, but the value of the clsvar in the class does not change. The important thing is said three times: the property search order of an object follows the order of finding the instance object itself first, then the class, and then the parent class of the class. When ins1 finds clsvar in its _ _ dict__, it no longer looks up, so it outputs a value of 20. At this point, however, the value of clsvar in the cls class is still 1.

Thank you for reading, the above is the content of "how to use the _ _ dict__ attribute in python". After the study of this article, I believe you have a deeper understanding of how to use the _ _ dict__ attribute in python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report