We will learn about inheritance in this article which we have already discusssed in the previous blog of OOPs in Python for Data Science. Inheritance is very important in terms of its uses. As sometimes, we do not require to access all of the information in one go so we can inherit the properties as and when required.
One of the major advantages of OOP is re-use. In the case of Inheritance, a class is inherited by another class. The subclass then adds some of its attributes to the superclass.
Checking if a class is a subclass of another:
In Python object is root of all class.
When we use Python 3.x, “class Test(object)” and “class Test” are the same.
When we use Python 2.x “class Test(object)” makes the object as a parent and “class Test” creates old style class.
Multiple Inheritance and Python:
Python supports multiple inheritance. All parent classes are specified as comma separated list in bracket.
Accessing parent members in a subclass:
- Using Parent Class name:
- Using Super:
We will look at some of the examples to make things clear.