Python
Programming
Object-oriented programming
Inheritance
Parent-Child classes

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:

python
1class Parent:
2    def my_method(self):
3        print("Method in Parent")
4
5class Child(Parent):
6    pass
7
8# Create an instance of Child
9child_instance = Child()
10child_instance.my_method()  # Outputs: Method in Parent

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:

python
1class Parent:
2    def my_method(self):
3        print("Method in Parent")
4
5class Child(Parent):
6    def my_method(self):
7        super().my_method()  # calling the parent class's method
8        print("Extension in Child")
9
10# Create an instance of Child
11child_instance = Child()
12child_instance.my_method()

This will output:

 
Method in Parent
Extension in Child

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:

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

In the multiple inheritance example above, calling method() on C instance outputs:

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

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:

MethodUse CaseWhen 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.


Course illustration
Course illustration

All Rights Reserved.