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

 

 

What are the Benefits of Super() Function in Python

We have already mentioned that super() function in Python is used to call method of a parent class from within a subclass. it has several benefits:

  1. Easy method overriding: super() function makes it easy to override method of a parent class and then call the original method from the subclass. this is useful when you want to modify the behavior of method without completely replacing it.
  2. Avoiding duplicate code: super() function allows you to avoid duplicating code in both parent and child classes. instead you can define the method in the parent class and then use super() to call it from the child class.
  3. Dynamic dispatch: super() function uses dynamic dispatch it means that it looks up method in the inheritance hierarchy at runtime. this allows you to change the behavior of the method by changing the order of the classes in the inheritance hierarchy.
  4. Code reuse: Using super() function promotes code reuse by allowing you to use method defined in a parent class in multiple subclasses.
  5. Improved maintainability: By using super() function you can reduce the amount of code you need to write and maintain, which can improve the maintainability of your code over time.

In result we can say that super() function is powerful and flexible tool for working with inheritance in Python. It allows you to easily override methods, avoid duplicating code, promote code reuse, and improve the maintainability of your code.