How do I call a parent class's method from a child class in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Python, inheritance is a fundamental concept where a class (referred to as a child or subclass) inherits attributes and methods from another class (known as a parent or superclass). One common requirement in working with inheritance is the ability to call a method from a parent class using a method in a child class. This can be particularly useful when extending or modifying the behavior of a parent class.
Understanding Basic Inheritance
Before we delve into the details of calling a parent class's method, let's first understand a simple inheritance scenario:
In the example above, Child class inherits my_method from Parent. Since Child does not have its own implementation of my_method, it will use the one from Parent.
Calling Parent Class Method Using super()
When you want to extend or alter the functionality of a method in a parent class, you use super() to call the parent class's method. super() returns a proxy object that delegates method calls to a parent or sibling class. This is particularly useful in the context of overriding.
Here’s how it's typically done:
This will output:
Why Use super()?
Using super() has several advantages:
- Maintains MRO (Method Resolution Order): This is particularly important in multiple inheritance, as
super()will handle method definitions smartly, ensuring each is called in the appropriate order. - Encourages modularity and code reuse: Allows child classes to leverage functionality in parent classes without having to rewrite those methods, while also permitting augmentation of those methods.
Advanced Usage of super()
super() becomes significantly powerful in complex class hierarchies involving multiple inheritance. Here's a quick look into such a scenario:
In the multiple inheritance example above, calling method() on C instance outputs:
This demonstrates how super() navigates through the Method Resolution Order (MRO).
Summary Table
Here's a table summarizing when and how to use parent class methods in a child class:
| Method | Use Case | When to Use |
Direct call (e.g., Parent.my_method(self)) | Directly invoking a method from a specific parent class. | When method overriding is not a concern and you need certainty of which class in the hierarchy you call. |
super().my_method() | To ensure the next method in the MRO is called. | In most inheritance scenarios, especially where proper handling of MRO and cooperative multiple inheritance is desired. |
Conclusion
Calling a parent class’s method from a child class in Python is a core aspect of working effectively with class inheritance. Utilizing super() is generally the best practice as it respects the method resolution order and supports the DRY (Don't Repeat Yourself) principle by not requiring methods to be redefined if they are already defined in a parent class. Understanding these concepts can significantly streamline the process of extending and maintaining Python applications with deep or complex class hierarchies.

