|
Exit:
In class B
In the above example, methods are called from class B but not from class A, and this is due to the Method Resolution Order (MRO).
The order that follows in the above code is — class B - & gt; class A
With multiple inheritance, methods are executed in the order specified when classes are inherited. For languages that support single inheritance, the order of resolution of methods is not interesting, but languages that support the order of resolution of multiple inheritance methods are very important. Let`s look at another example to get a deeper understanding of the method resolution order:
|
Output:
In class B
In the above example, we use multiple inheritance, and it is also called Diamond inheritance or Deadly Diamond of Death and looks like this:

Python follows the depth-first search order and therefore ends up calling a method from class A. Following the method resolution order, the search order looks like this way.
Class D - & gt; Class B - & gt; Class C - & gt; Class A
Python follows first order depth for resolving methods and attributes. So in the above example, it executes the method in class B.
Old and new style of ordering:
In the older version of Python (2.1), we are required to use the classes old style, but in Python (3.x and 2.2) we are required to use only new classes. The new style classes are those whose first parent class inherits from the root Python object class.
|
Method Resolution Order (MRO) is different in both declaration styles. The old style classes use the DLR algorithm or left to right left to right, while the new style classes use the C3 linearization algorithm to resolve the method across multiple inheritance.
DLR Algorithm
While implementing multiple inheritances, Python creates a list of classes to look for because it needs to determine which method should be called when the instance is called. As the name suggests, the order of resolving the method will first look for depth and then go from left to right. For example
|
In the above Example, the algorithm first looks at the instance class for the called method. If it is not there, then it looks at the first parent, if it is also not there, then the parent-parent is considered. This continues to the end of the class depth and finally to the end of the inherited classes. So the order of resolution in our last example will be D, B, A, C, A. But, A cannot be represented twice, so the order will be D, B, A, C. But this algorithm changes in different ways and demonstrating different behavior at different times. So Samuele Pedroni first discovered the inconsistency and presented the C3 linearization algorithm.
C3 linearization algorithm:
C3 linearization algorithm — it is an algorithm using new style classes. It is used to resolve the inconsistency created by the DLR algorithm. It has certain restrictions:
- Children precede their parents
- If a class inherits from multiple classes, they are stored in the order specified in the base class tuple.
- The inheritance graph defines the structure of how methods are resolved.
- The user should only visit the superclass after visiting a local class method.
- Monotony
The C3 linearization algorithm works according to three rules:
Methods for the method resolution order (MRO) of a class:
To get the resolution order of a class method we can use the mro ()
or mro ()
attribute. Using these methods, we can display the order in which the methods are allowed. For example
|
Exit :
C constructor (& lt; class & # 39; __ main __. C & # 39; & gt ;, & lt; class & # 39; __ main __. A & # 39; & gt ;, & lt; class & # 39; __ main __. B & # 39; & gt ;, & lt; class & # 39; object & # 39; & gt;) [& lt; class & # 39; __ main __. C & # 39; & gt ;, & lt; class & # 39; __ main __. A & # 39; & gt ;, & lt; class & # 39; __ main __. B & # 39; & gt ;, & lt; class & # 39; object & # 39; & gt;]