How does Python's super() work with multiple inheritance?
In Python, super() is used to call a method from a parent class.
In Python, super() is used to call a method from a parent class. When used with multiple inheritance, Python follows the Method Resolution Order (MRO) to determine which parent class method to call. By default, you should use super() without arguments to let Python handle the MRO automatically.
An example of super() in Python classes
class Parent1:
def method(self):
print("Parent1 method")
class Parent2:
def method(self):
print("Parent2 method")
class Child(Parent1, Parent2):
def method(self):
super().method()
c = Child()
c.method()
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
This will output:
Parent1 methodThe zero-argument super() in Python 3 is syntactic sugar for super(CurrentClass, self). It automatically resolves the method using the class's MRO, searching for the method in the next class in the inheritance chain. This ensures cooperative multiple inheritance works correctly. Explicitly passing parent classes to super() (e.g., super(Child, self).method()) still respects the MRO but starts the search from the specified class. It is generally not recommended unless you have a specific reason to override the default resolution order. Note that super() also forwards arguments to the parent method, which is common in real-world inheritance.