Python basic data structure dict inherits from object, but why is it a subclass of MutableMapping

dict is a subclass of the abstract class MutableMapping

>>> from collections.abc import MutableMapping
>>> isinstance(dict(),MutableMapping)
True

but you can see in builtin.py that dict inherits from the object object.

so I wonder how dict inherits MutableMapping, whether it inherits directly or has some intermediate links.
does MutableMapping inherit object, and what is the relationship between the three?

I learned that the implementation of dict should be implemented internally by the interpreter.
I know that this probably involves the inner workings of the python interpreter, and I"m interested in it.

Is

MutableMapping also implemented internally by the interpreter?
then dict and MutableMapping implement inheritance relationships within the interpreter

Nov.13,2021

isinstance not only based on the mro of the class, but also based on the duck protocol to determine whether the object satisfies a certain type of behavior. For example, the list object inherits directly from object. But it can also fall, it is an iterable object. True is returned via isinstance ([], typing.Iterable) . This section is related to the _ instancecheck__ magic method. According to the description of python.org/dev/peps/pep-3119/" rel=" nofollow noreferrer "> PEP 3119 , we know that the check of the instance is allowed to overload.

The primary mechanism proposed here is to allow overloading the built-in functions isinstance () and issubclass (). The overloading works as follows: The call isinstance (x, C) first checks whether C.__instancecheck__ exists, and if so, calls C.__instancecheck__ (x) instead of its normal implementation. Similarly, the call issubclass (D, C) first checks whether C. scheduled subclasschecklists _ exists, and if so, calls C. scheduled subclasschecklists _ (D) instead of its normal implementation.

roughly means that when calling isinstance (XMagazine C) , check whether C.powered _ instancecheck__ exists, and if so, call C.encoding _ instancecheck__ (x) instead of its normal implementation (judging by the mro of the class).

MutableMapping should also realize the judgment of "subclass" by overloading.

related articles: Inspection mechanism of in-depth profiling isinstance


because python's built-in dict class doesn't actually inherit the MutableMapping class, how can a built-in type written in C inherit from an abstract base class written in python? Check the MRO method parsing list of the dict class, and its "real" base class is clear:

>>> dict.__mro__
(<class 'dict'>, <class 'object'>)

so why does the function check dict,python tell you that "dict is a subclass of MutableMapping"?

>>> import collections
>>> issubclass(dict, collections.abc.MutableMapping)
True

because the abstract base class of python can [register a virtual subclass]: when you import collections a package, the code of the package has already executed the MutableMapping.register () method, registering dict as its own virtual subclass, so it can pass the function check.

want to study the behavior of dict and its inheritance relationship from the perspective of python code? No problem, the official python code version of the dict class, the UserDict class, is designed for programmers to inherit / modify custom mapping types.

>>> from collections import UserDict
>>> UserDict.__mro__
(<class 'collections.UserDict'>, <class 'collections.abc.MutableMapping'>, <class 'collections.abc.Mapping'>, <class 'collections.abc.Collection'>, <class 'collections.abc.Sized'>, <class 'collections.abc.Iterable'>, <class 'collections.abc.Container'>, <class 'object'>)

The object in

python is a "duck type", which is too flexible to determine whether an object is an instance of a class or not.
collections.abc means abstract base class . abc constrains and defines the basic characteristics of the class , so you can use isinstance and issubclass to determine instances and subclasses.

abc

Menu