One of the important OOP feature is Inheritance in Python. When a class inherits some or all of the behaviours from another class is known as Inheritance. In such a case, the inherited class is the sub class and the latter class is the parent class.
In an inherited subclass, a parent class can be referred to with the use of the
super() function. The super function returns a temporary object of the superclass that allows access to all of its methods to its child class.
Note: For more information, refer to Inheritance in Python
Furthermore, The benefits of using a super function are:-
- Need not remember or specify the parent class name to access its methods. This function can be used both in single and multiple inheritances.
- This implements modularity (isolating changes) and code reusability as there is no need to rewrite the entire function.
- Super function in Python is called dynamically because Python is a dynamic language unlike other languages.
There are 3 constraints to use the super function:-
- The class and its methods which are referred by the super function
- The arguments of the super function and the called function should match.
- Every occurrence of the method must include
super()after you use it.
Super function in single inheritance
Example: Let’s take an example of animals. Dogs, cats and cows are part of animals. They also share common characteristics like –
- They are mammals.
- They have tail and four legs.
- They are domestic animals.
So, the classes dogs, cats and horses are subclass of animal class. This is an example of single inheritance because many subclass are inherited from a single parent class.
It is a mammal. Has legs and tail
Super function in multiple inheritance
Example: Let’s take another example. Suppose a class canfly and canswim inherits from a mammal class and these classes are inherited by the animal class. So the animal class inherits from the multiple base classes. Let’s see the use of super in this case.
Dog cannot fly Dog cannot swim Dog Is a mammal
The class Animal inherits from two-parent classes – canFly and canSwim. So, the subclass instance Carol can access both of the parent class constructors.
- Python | super() in single inheritance
- Python | super() function with multilevel inheritance
- Calling a Super Class Constructor in Python
- OOP in Python | Set 3 (Inheritance, examples of object, issubclass and super)
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python - Read blob object in python using wand library
- Reading Python File-Like Objects from C | Python
- Python | Merge Python key values to list
- twitter-text-python (ttp) module - Python
- MySQL-Connector-Python module in Python
- Python | Convert list to Python array
- Python | PRAW - Python Reddit API Wrapper
- Important differences between Python 2.x and Python 3.x with examples
- Python | Index of Non-Zero elements in Python list
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python