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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Person: def __init__(self): print("This is person base class") class Student(Person): def __init__(self): print("This is student derived class ") st = Student() |
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](https://geekscoders.com/wp-content/uploads/2020/11/python-super-function.jpg)
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Person: def __init__(self): print("This is person base class") class Student(Person): def __init__(self): super().__init__() print("This is student derived class ") st = Student() |
Run the code and you will see that it is calling the base class initializer method.
![Super() Function Example in Python](https://geekscoders.com/wp-content/uploads/2020/11/python-super-function-example.jpg)
Now let’s create another example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Person: def __init__(self, name, age): self.name = name self.age = age class Student(Person): def __init__(self, name, age, email): super().__init__(name, age) self.email = email st = Student("Geekscoders", 20, "geekscoders@gmail.com") print(st.name) print(st.age) print(st.email) |
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](https://geekscoders.com/wp-content/uploads/2020/11/super-function-in-python.jpg)
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:
- 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.
- 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.
- 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.
- Code reuse: Using super() function promotes code reuse by allowing you to use method defined in a parent class in multiple subclasses.
- 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.