In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces Python magic function missing how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone will have a harvest after reading this Python magic function missing how to use the article, let's take a look at it.
1. Some value__missing__()
When taking values from ordinary dictionaries, there may be cases where the key does not exist:
dd = {'name':'PythonCat'}dd.get('age') #Result: Nonedd.get ('age ', 18) #Result: 18dd ('age ') #Error KeyErrordd.__ getitem__('age ') #is equivalent to dd ['age']
For the get() method, it has a return value, and can pass a second parameter as the return content when the key does not exist, so it can also accept. However, the other two types of writing are wrong.
In order to solve the latter two writing problems, you can use the__missing__() magic method.
Now, suppose we have a request that takes the value of a key from the dictionary, returns the value if it has a value, inserts the key if it has no value, and gives it a default value (e.g., an empty list).
It's not easy to implement with native dict, but Python provides a very useful extension class collections.defaultdict:
As shown in the figure, when a key that does not exist is taken, KeyError is not reported, but is stored in the dictionary by default.
Why does defaultdict do this?
The reason is that defaultdict, after inheriting the built-in type dict, also defines a__missing__() method. When__getitem__takes a value that does not exist, it will call the factory function passed in the input parameter (the above example is to call list() to create an empty list).
As the most typical example, defaultdict writes in a document comment:
In short, the main purpose of__missing__() is to be called by__getitem__when the key is missing, thus avoiding KeyError.
Another typical example is collections.Counter, which is also a subclass of dict. When taking an uncounted key, it returns a count of 0:
2. ghostly__missing__()
From the above,__missing__() will be called when__getitem__() does not get a value, but I also inadvertently found a detail: __getitem__() does not necessarily call__missing__() when it does not get a value.
This is because it is not a required attribute of built-in types and is not predefined in dictionary base classes.
AttributeError: type object 'object' has no attribute '__missing__'
Using dir() to check, we found that this attribute does not exist:
If you look inside dict's parent class, object, you'll see the same result.
What was going on? Why is there no__missing__attribute in dict and object?
However, looking at the latest official documentation, object clearly contains this attribute:
Source: docs.python.org/3/reference/datamodel.html? highlight=__missing__#object.__ missing__
That is,__missing__is theoretically predefined in the object class, and its documentation proves this, but it is not actually defined! The document deviated from reality!
Thus, when a subclass of dict (e.g. defaultdict and Counter) defines__missing__, the magic method actually belongs only to that subclass, i.e., it is a magic method born in that subclass!
From this, I have a premature guess: __getitem__() will determine whether the current object is a subclass of dict, and whether it has__missing__(), and then call it (if the parent class also has this method, it will not be determined first, but will be called directly).
I said this conjecture in the communication group, and some students quickly found verification in CPython source code:
And this is interesting. Magic methods that exist only on subclasses of built-in types are hard to find throughout the Python world.
I suddenly have an association: this ghostly__missing__(), like a magician who is good at playing "big change living person," first let the audience see him through the glass outside (i.e. official document), but when the door is opened, he is not inside (i.e. built-in type), and then change the props, he appears intact again (i.e. subclass of dict).
3. Enchanted__missing__()
The magic of__missing__(), in addition to its own "magic", it also needs a strong "magic" to drive.
In the previous article, I found that native magic methods are independent of each other. They may have the same core logic in the C language interface, but in the Python language interface, there is no call relationship:
This dead-end behavior of magic methods violates general code reuse principles and is the reason why subclasses of built-in types behave strangely.
Official Python would rather provide new UserString, UserList, UserDict subclasses than reuse magic methods, and the only reasonable explanation seems to be that making magic methods call each other is too expensive.
However, for the special case__missing__() Python has to compromise and pay this price!
__missing__() is a "second-class citizen" of magic methods. It has no independent call entry and can only be called passively by__getitem__(), that is,__missing__() depends on__getitem__().
Unlike "first-class citizens," such as__init__(),__enter__(),__len__(),__eq__(), etc., which are triggered either at a node in the object lifecycle or execution process, or by a built-in function or operator, these are relatively independent events and have no dependencies.
__missing__() depends on__getitem__() for method calls, and__getitem__() depends on__missing__() for full functionality.
To do this,__getitem__() opens a backdoor in the interpreter code, folding back from the C interface to the Python interface to call that particular method called "__missing__."
And this is the real magic, so far,__missing__() seems to be the only magic method to enjoy this treatment!
4. Summary
Python dictionaries provide two built-in methods for values,__getitem__() and get(), which handle values differently when they don't exist: the former reports an error KeyError, while the latter returns None.
Why does Python provide two different methods? Or, why does Python want these two methods to do different things?
There may be a complex (or simple) explanation for this, but I won't delve into it for the time being.
But one thing is certain: the primitive dict type simply throws KeyError crudely.
To make dictionary types behave more strongly (or to make__getitem__() behave like get()), Python lets dictionary subclasses define__missing__() for__getitem__() lookup calls.
In this paper, the implementation principle of__missing__() is sorted out, and it is revealed that it is not an insignificant existence, on the contrary, it is the only special case that breaks the barrier between magic methods and supports being called by other magic methods!
Python takes pains to introduce derived classes UserString, UserList, UserDict to maintain the independence of magic methods, but it compromises__missing__().
About "Python magic function missing how to use" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of "how to use Python magic function missing" knowledge. If you still want to learn more knowledge, please pay attention to the industry information channel.
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.