Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Master Python Multiple Inheritance, Multilevel Inheritance, and Method Resolution Order (MRO)

Python Multiple Inheritance

Explore how Python supports multiple inheritance, the nuances of multilevel inheritance, and how the method resolution order (MRO) governs attribute lookup.

Python Multiple Inheritance

Python allows a class to inherit from more than one base class, just as C++ does. This feature is known as multiple inheritance and lets a derived class combine behavior from several parent classes.

The syntax mirrors single inheritance: you list all parent classes in parentheses, separated by commas.

Example

class Base1:
    pass

class Base2:
    pass

class MultiDerived(Base1, Base2):
    pass

In this example, MultiDerived inherits attributes and methods from both Base1 and Base2.

Master Python Multiple Inheritance, Multilevel Inheritance, and Method Resolution Order (MRO)

Diagram: MultiDerived receives features from Base1 and Base2.


Python Multilevel Inheritance

Beyond two levels, you can chain inheritance: a class inherits from a derived class, creating a multilevel inheritance hierarchy. Python supports any depth.

Each new derived class inherits from its immediate parent, while also carrying forward the ancestor’s attributes.

Example:

class Base:
    pass

class Derived1(Base):
    pass

class Derived2(Derived1):
    pass

Here, Derived1 extends Base, and Derived2 further extends Derived1, forming a three‑level chain.

Master Python Multiple Inheritance, Multilevel Inheritance, and Method Resolution Order (MRO)

Method Resolution Order (MRO) in Python

Every Python class ultimately inherits from object, the universal base type. Consequently, all objects are instances of object, whether built‑in or user‑defined:

# Output: True
print(issubclass(list,object))

# Output: True
print(isinstance(5.5,object))

# Output: True
print(isinstance("Hello",object))

When multiple inheritance is involved, Python resolves attributes using a depth‑first, left‑to‑right traversal, without revisiting a class twice. This traversal order is called the Method Resolution Order (MRO) and guarantees that a class appears before its parents.

The MRO for MultiDerived is: [MultiDerived, Base1, Base2, object]. You can view it via the __mro__ attribute or the mro() method:

MultiDerived.__mro__
# (<class '__main__.MultiDerived'>, <class '__main__.Base1'>, <class '__main__.Base2'>, <class 'object'>)

MultiDerived.mro()
# [<class '__main__.MultiDerived'>, <class '__main__.Base1'>, <class '__main__.Base2'>, <class 'object'>]

Complex inheritance graphs follow the same principle. Consider this example:

Master Python Multiple Inheritance, Multilevel Inheritance, and Method Resolution Order (MRO)
# Demonstration of MRO
class X:
    pass

class Y:
    pass

class Z:
    pass

class A(X, Y):
    pass

class B(Y, Z):
    pass

class M(B, A, Z):
    pass

print(M.mro())
# Output:
# [, , , , , , ]

To delve deeper into the algorithm that calculates MRO, consult the official Python documentation on class inheritance and the discussion on MRO.

Python

  1. Mastering C# Inheritance: Concepts, Types, and Practical Code
  2. Master C++ Inheritance: Build Powerful Classes with Reusable Code
  3. C++ Inheritance Models: Multiple, Multilevel, Hierarchical
  4. Mastering Python Custom Exceptions: A Practical Guide
  5. Mastering Python Inheritance: Concepts, Syntax, and Practical Examples
  6. Python OOP Fundamentals: Classes, Objects, Inheritance, and Constructors Explained
  7. Mastering Python Class Slots: Optimize Memory & Speed
  8. How to Return Multiple Values in Python: A Simple Guide
  9. Mastering C# Inheritance: Build Reusable, Maintainable Code
  10. Master Python's Object-Oriented Programming: A Comprehensive Guide