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.

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.

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:

# 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
- Mastering C# Inheritance: Concepts, Types, and Practical Code
- Master C++ Inheritance: Build Powerful Classes with Reusable Code
- C++ Inheritance Models: Multiple, Multilevel, Hierarchical
- Mastering Python Custom Exceptions: A Practical Guide
- Mastering Python Inheritance: Concepts, Syntax, and Practical Examples
- Python OOP Fundamentals: Classes, Objects, Inheritance, and Constructors Explained
- Mastering Python Class Slots: Optimize Memory & Speed
- How to Return Multiple Values in Python: A Simple Guide
- Mastering C# Inheritance: Build Reusable, Maintainable Code
- Master Python's Object-Oriented Programming: A Comprehensive Guide