How Inheritance is working without super
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-12-46ffda859ba6> in <module> 24 25 # calling parent class function ---> 26 Emp_details.name_, Emp_details.name AttributeError: 'Emp' object has no attribute 'name'
In above code Emp class is inherits the Person class
- Both class has the __init__ constructor
- trying to call name & name_ from other class
- But we will encounter the above error
With super implementation in inherent we can solve the problem. example can found below
The Python super() function returns objects represented in the parent’s class and is very useful in multiple and multilevel inheritances to find which class the child class is extending first.
Syntax of super() in Python
Return : Return a proxy object which represents the parent’s class.
Example of super() function in Python
The ID is: 103 The Name is: Suraj kr gupta The Address is: Noida The Emails is: KKK@gmails
Solving the first problem using super
Understanding Python super() with __init__() methods
Python has a reserved method called “__init__.” In Object-Oriented Programming, it is referred to as a constructor. When this method is called it allows the class to initialize the attributes of the class. In an inherited subclass, a parent class can be referred 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
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.
Super function in single inheritance
Let’s take the example of animals. Dogs, cats, and cows are part of animals. They also share common characteristics like –
- They are mammals.
- They have a tail and four legs.
- They are domestic animals.
So, the classes dogs, cats, and horses are a subclass of animal class. This is an example of single inheritance because many subclasses is inherited from a single parent class.
It is a mammal. Has legs and tail
Super function in multiple inheritances
Let’s take another example of a super function, Suppose a class canfly and canswim inherit 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 Python super with arguments in this case
The class Animal inherits from two-parent classes – canFly and canSwim. So, the subclass instance Carol can access both of the parent class constructors.
Dog cannot fly Dog cannot swim Dog Is a mammal
MRO in Multiple Inheritance
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>) [<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>]
Let’s take another example of a super function, suppose a class canSwim is inherited by canFly, canFly from mammal class. So the mammal class inherits from the Multi-Level inheritance. Let’s see the use of Python super with arguments in this case
Dog cannot swim Dog cannot fly Dog Is a mammal
Please Login to comment...