Inheritance Order
Breadth First principle:
classA:PassclassB (A):PassclassC (A):PassclassD (B):PassclassE (C):PassclassF (D, E):PassPrint("f.__mro__ ="F.__mro__)Print("F.mro () =", F.mro ())
View Code
Principles of inheritance
How Python actually implements inheritance:
- For each class defined, Python calculates a list of method resolution orders (MRO), which is a simple linear sequential list of all base classes.
- To implement inheritance, Python finds the base class from left to right on the MRO list until the first class that matches the property is found.
- The construction of the MRO list is implemented by a C3 linearization algorithm. We're not going to delve into the math of this algorithm, it's actually merging all of the parent's MRO lists and following three guidelines:
- Subclasses are checked before the parent class.
- Multiple parent classes are checked according to their order in the list.
- If there are two legitimate choices for the next class, select the first parent class.
Python New class inheritance Order