Python Super() Function

In this article we want to learn about Python Super() Function , so python super() function is used for accessing the methods and properties of the base class or super class, basically a super function returns a proxy object, or we can say In Python, super() is built-in function that returns temporary object of superclass, allowing access to its methods and attributes. it is commonly used in object-oriented programming to call method in parent or superclass while making sure that all necessary setup and cleanup operations are performed.

super() function is typically used inside subclass to call method defined in its parent class. by using super(), the subclass can inherit the functionality of the parent class while also adding its own behavior.

 

 

Benefits of Python Super Function

The benefits of using super() function in Python are:

  1. Code reusability: super() function allows subclass to inherit and reuse the code from its parent class, avoiding duplication of code and reducing the amount of code that needs to be written.
  2. Dynamic method resolution: super() enables dynamic method resolution, which means that if the inheritance hierarchy changes, the same super() call can be used to call the updated method in the new parent class.
  3. Maintains method resolution order: super() function is aware of the method resolution order (MRO) of class, so it can ensure that the correct method is called in the correct order in the inheritance hierarchy.
  4. Prevents hardcoding class names: Using super() avoids hardcoding the names of parent classes and it makes the code more flexible and easier to maintain.

So we can say that super() function is an important tool for object oriented programming in Python, enabling inheritance, code reuse and dynamic method resolution. it can make code more flexible and easier to maintain and helps to avoid errors that can arise from hardcoding class names or method calls.

 

 

Now we want to create an example in Python Super() Function.

In the 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 method we are just printing some texts. the second class is Employee 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.

 

 

 

Run the code and this will be the result, even though we are extending from the Person class, but by instantiating the Employee class it is not calling the base class __init__ 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.

 

 

 

 

If you run the code this will be the result and you can see that now it is calling our base class method at the first.

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

 

 

 

Let’s create another example.

 

In this code, Person is a super (parent) class, while Employee 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.

 

 

 

 

 

Leave a Comment

Share via
Copy link
Powered by Social Snap