Python super() - Python 3 super() With Examples

Python super() function allows us to refer to the parent class explicitly. It’s useful in case of inheritance where we want to call super class functions.

Python super

To understand about python super function, you have to know about Python Inheritance. In Python Inheritance, the subclasses inherit from the superclass.

Python super() function allows us to refer the superclass implicitly. So, Python super makes our task easier and comfortable. While referring the superclass from the subclass, we don’t need to write the name of superclass explicitly. In the following sections, we will discuss python super function.

Python super function example

At first, just look at the following code we used in our Python Inheritance tutorial. In that example code, the superclass was Person and the subclass was Student. So the code is shown below.

In the above example, we have called parent class function as:

We can replace this with python super function call as below.

The output will remain the same in both the cases, as shown in the below image.

Python super() - Python 3 super()

Python 3 super

Note that the above syntax is for python 3 super function. If you are on python 2.x versions, then it’s slightly different and you will have to do the following changes:

The first change is to have object as the base class for Person. It’s required to use the super function in Python 2.x versions. Otherwise, you will get the following error.

The second change in the syntax of the super function itself.

As you can see that python 3 super function is a lot easier to use and the syntax is also clean looking.

Python super function with multilevel inheritance

As we have stated previously that Python super() function allows us to refer the superclass implicitly.

But in the case of multi-level inheritances which class will it refer? Well, Python super() will always refer the immediate superclass.

Also Python super() function not only can refer the __init__() function but also can call all other function of the superclass. So, in the following example, we will see that.

Let’s see the output of above python 3 super example with multi-level inheritance.

So, from the output we can clearly see that the __init__() function of class C had been called at first, then class B and after that class A. Similar thing happened by calling sub_method() function.

Why do we need Python super function

If you have previous experience in Java language, then you should know that the base class is also called by a super object there. So, this concept is actually useful for the coders. However, Python also keeps the facility for the programmer to use superclass name to refer them. And, if your program contains multi-level inheritance, then this super() function is helpful for you.

So, that’s all about python super function. Hopefully, you understood this topic. Please use the comment box for any query.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

By admin

Leave a Reply