Python super() Function Tutorial

In this section, we will learn about the super() function in Python.

Note: we’re assuming you already familiar with the Python inheritance.

What is Python super() Function?

The `super()` function is used to call a parent’s method and invoke that within the body of a child method.

This is especially helpful when we have the `__init__` method defined in a parent class! Because when creating an object from a child class that has a parent class with explicitly defined `__init__` method, it is the responsibility of the child class to initialize the attributes of its parent class as well.

Example: python call super() method in __init__ method

class Parent: 

    def __init__(self, firstName, lastName):
        self.firstName = firstName
        self.lastName = lastName

    def printFullName(self):
        print(f"{self.firstName} {self.lastName}")


class Child (Parent):

    def __init__(self, firstName, lastName):
        super().__init__(firstName, lastName)


child = Child("John","Doe")

child.printFullName()

Output:

John Doe

In this example, the `Child` class is the derived class from the `Parent` class.

We can see the Parent class has explicitly defined the `__init__` method with two parameters. So in order to initialize the attributes of the Parent class, we need to call this method using the `super()` function within the body of the `__init__` method of the Child class and pass the required arguments. That way, python knows with what values it should initialize the attributes of the Parent class.

Python super() Function in Instance Methods

The use of the `super()` function is not limited to just the `__init__` method of a child class! In fact, we’re allowed to use this function within the body of any method of a child class in order to invoke any method of the parent class!

Example: using super() function in instance methods

class Parent: 

    def __init__(self, firstName, lastName):
        self.firstName = firstName
        self.lastName = lastName

    def printFullName(self):
        print(f"{self.firstName} {self.lastName}")


class Child (Parent):

    def __init__(self, firstName, lastName):
        super().__init__(firstName, lastName)

    def sayHi(self):
        print("Hello from the Child class")
        super().printFullName()

child = Child("John","Doe")

child.sayHi()

Output:

Hello from the Child class

John Doe

Here, as you can see, we have called the `printFullName()` method of the Parent class within the body of the `sayHi()` method of the Child class.

Python super() Function and Multiple Inheritance

Calling the `super()` function and then invoking a method after that means we want to call the target method but we don’t care which ancestor class has the target method!

If a child class is involved in a multiple or multi-level inheritance, the python interpreter will check the immediate parent class for the mentioned method! If that parent class had the mentioned method, then it will be invoked. But if the method wasn’t there, then the search will move to the next immediate parent! Basically, based on the order of parent classes, Python search its way for the target method until it finds one and invoke it.

Note: if the invoked method was not in any class, then an error will return.

Example: super() function in python and multiple inheritance

class GrandParent:
    def grandParentMessage(self):
        print("A message from the grandpa :)")

class Parent (GrandParent): 

    def __init__(self, firstName, lastName):
        self.firstName = firstName
        self.lastName = lastName

    def printFullName(self):
        print(f"{self.firstName} {self.lastName}")


class Child (Parent):

    def __init__(self, firstName, lastName):
        super().__init__(firstName, lastName)

    def sayHi(self):
        print("Hello from the Child class")
        super().printFullName()

child = Child("John","Doe")

child.grandParentMessage()

Output:

A message from the grandpa 🙂

Note that the `grandParentMessage()` method is not in the parent of the `Child` class but instead is in the grand parent of this class. But it doesn’t matter! Because Python execution engine will check the entire ancestor classes in order to find the called method.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies