In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what is the use of _ _ dict__ in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The biggest advantage of the programming language Python is that the dynamic nature of the language brings programming convenience, which is not as rigid as the static language, for example, it can dynamically add properties that are not available when the class definition is not available at run time, such as:
Class A: def _ init__ (self): self.name = 'zhang' def eat (self): print (' eat') a = A () a.age = 10print (a.age)
This code cannot be compiled in Java, but such operations are no longer common in Python, so what exactly does the Python interpreter do behind it? The answer is that all built-in (build-in) data types (type) in _ _ dict__,Python have attributes _ _ dcit__, and of course custom classes (class), because custom classes are also a data type. You can get the result by adding a line print a. Before and after the a.age=10.
{'name':' zhang'} {'age': 10,' name': 'zhang'} 10
It is not difficult to see that the instance object properties defined during the run time and class definition period are placed in the _ _ dict__ property of the object, followed by a few lines of code after aura ():
A.country = "China" print a.countryprint a.
The results are obtained:
China {'age': 10,' name': 'zhang'}
There is no 'country'' property in the _ _ dict__ property of object a, so where did it come from? Then you might as well print the _ _ dict__ of class object A to have a look:
Print a.countryprint a.__dict__print A.__dict__
Output result:
China {'age': 10,' name': 'zhang'} {' country': 'China',' _ module__':'_ main__', 'eat':,... Omit}
You should understand that at run time, the properties of the defined instance object (a) are placed in a. Instance object (A), while the properties of the defined class object (A) are placed in A. instance object (A), so how does the instance object an access the country property? It is agreed in Python that when accessed with obj.attr, it looks for it in the following order:
Object itself, obj.__dict__ ['attr']
Object type, obj.__class__.__dict__ ['attr']
The base class of the object type, all _ _ dict__ ['attr'] in obj.__class__.__bases__. Note that when there is diamond inheritance in the case of multiple inheritance, Python searches in the order determined by MRO.
If the base class has not yet found the property to access, the Python interpreter will run an exception: AttributeError. So the question is, can we add some custom attributes to the build-in type? For example:
List.myExtension = lambda self,x:x * 2list.myExtension (10)
When running, the interpreter reports an error: TypeError: can't set attributes of built-in/extension type 'list', can you add a value to the _ _ dict__ attribute of list?
List.__dict__ ['myExtension'] = lambda self, XRV * 2
Again, it doesn't work: TypeError: 'dictproxy' object does not support item assignment, if you think about it, it makes sense that the latter doesn't work, and the two are equivalent, so why not allow programmers to extend built-in types?
Answer the sleeping classmate behind, and answer: father Guido van Rossum realizes these types in C language, and you can no longer modify them, so how to extend the existing types gracefully? Students who know OOP will do it in the way of inheritance. So the classmate went back to sleep.
This is the end of the content of "what is the use of _ _ dict__ in Python"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.