Python
Programming
Multiple Inheritance
super() function
Object-Oriented Programming

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.

Practice OOD

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:

python
1class A:
2    def method(self):
3        print("A method")
4    
5class B(A):
6    def method(self):
7        print("B method")
8        super().method()
9
10class C(A):
11    def method(self):
12        print("C method")
13        super().method()
14
15class D(B, C):
16    def method(self):
17        print("D method")
18        super().method()

Now, if you create an instance of D and call method:

python
d = D()
d.method()

The output will demonstrate the order in which methods are called based on the MRO:

 
1D method
2B method
3C method
4A method

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

FeatureDescription
Syntaxsuper().method_name()
BehaviorCalls the next method in the MRO.
Best UsedMultiple inheritance scenarios and overriding methods.
AdvantagesMaintainability, 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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Practice OOD

All Rights Reserved.