Super() Function in Python

In this article we are going to talk about Super() Function in Python,

Python Super() Function is used for accessing to the methods and properties

of the parent class or base class, and super function returns a proxy object.

 

Now let’s create a practical example on Python Super() function.

 

in the above code we have two classes, the first class is Person class and it is our

parent class, we have our initializer method in this class, also in the initializer we are

just printing some texts in the console. the second class is Student class and it is a child

class of the Person class, and in here also we are going to print something in the console.

now if you run this code you will see that it is not calling the base class initializer method,

but it is calling the derived class initializer method.

 

 

This is the result and as we can see it is calling the derived class initializer method.

Super() Function in Python
Super() Function in Python

 

 

 

Now what if we want to call our base class initializer method, for that we

are going to use the super function in Python, let’s bring changes to our code

and add the super function.

 

 

 

Run the code and you will see that it is calling the base class initializer method.

Python Super() Function
Python OOP Tutorial

 

 

 

 

 

 

Let’s create another example

 

In this code, Person is a super (parent) class, while Student is a derived (child) class.

The usage of the super function allows the child class to access  the parent class’s init()

 property. In other words, super() allows you to build classes that easily extend the

functionality of previously built classes without implementing their functionality again.

 

 

 

Run the code and this is the result.

What is Super Function
What is Super Function

 

Leave a Comment