Python Tutorial
About Lesson

In this lesson we want to learn about Python Super() Function, using super function we can access to the methods and properties of the parent class or base class, basically a super function returns a proxy object.

 

 

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

 

So in the above example we have two classes, the first class is Person class and it is our base 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.

Python Super() Function
Python Super() Function

 

 

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.

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

 

 

 

Now 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.

Python OOP Tutorial
Python OOP Tutorial

 

Thank you for visiting my website! If you enjoyed the free courses, please consider supporting my works on Patreon.
Exercise Files
No Attachment Found
No Attachment Found