How does Python's super() work with multiple inheritance?
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Python's super() function is a built-in function that returns a proxy object (temporary object of the superclass) that allows you to call methods of the base class via delegation. This is useful in single inheritance to refer to the parent class without naming them explicitly, making the code more maintainable and adaptable to changes. However, its true power shines through in the context of multiple inheritance, where complex scenarios could arise due to the various relationships involved between classes.
Understanding Multiple Inheritance
Multiple inheritance allows a class to inherit from more than one base class. This can be incredibly useful, but it also introduces complexities and potential for conflicts, such as the diamond problem (where a class inherits from two classes that both inherit from a common base class).
How super() Works
In Python, super() is used to call the next method in the hierarchy line, following the method resolution order (MRO). The MRO determines the order in which base classes are searched when executing a method. Python uses the C3 linearization or C3 superclass linearization algorithm to compute the MRO in a way that preserves the left-to-right ordering specified in the class definition and is deterministic.
A Technical Example
Consider a scenario with the following class inheritance:
Now, if you create an instance of D and call method:
The output will demonstrate the order in which methods are called based on the MRO:
This happens because Python's MRO for class D will be [D, B, C, A]. The super() in each method() makes a call to the next class in this order.
Why and When to Use super()
Using super() in a multiple inheritance scenario ensures that all base classes are properly initialized and that the method overriding chains properly delegate calls up the inheritance chain. Here are some cases where you might want to use super():
- In the constructor (
__init__) methods to ensure all parent classes are initialized. - When overriding methods to extend or modify the behavior of inherited methods, ensuring every class in the hierarchy gets a chance to run its logic.
Benefits of Using super()
The use of super() brings several benefits:
- Maintainability: Changing the base classes doesn’t require changes to the method calls.
- Flexibility: It's easier to refactor code, as dependencies are minimized.
- DRY principle: Keeps the code dry by avoiding repetition of base class names and method names.
Summary Table
| Feature | Description |
| Syntax | super().method_name() |
| Behavior | Calls the next method in the MRO. |
| Best Used | Multiple inheritance scenarios and overriding methods. |
| Advantages | Maintainability, flexibility, adherence to DRY principle. |
Conclusion
super() is a powerful tool in Python, particularly useful in complex multiple inheritance scenarios. It helps to avoid direct base class references, which can make the code less flexible and harder to maintain. By correctly using super(), developers ensure a smooth method resolution path that respects the order and logic of the base classes.
Related reading
- How does Python's super work with multiple inheritance?
- How should I have explained the difference between an Interface and an Abstract class?
- How to Autowire services in SpringBoot
- How to avoid Dependency Injection constructor madness?
- How does sample_weight compare to class_weight in scikit-learn?
- How does sp_randint work?
- How to consume a Scoped service from a Singleton?
- How to create a bean using Bean annotation in Spring Boot for abstract class?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.