The only Python inheritance guide you need

Abhishek Bose
2 min readNov 29, 2020

Python is fantastic language for learning objected-oriented programming. An important pillar of objected-oriented programming is using inheritance in your code. Writing two classes which represent a similar real world object but bear not relationship with each other is actually on not much use.

This is the short blog on how to make the best use of classes in python.

Let’s look at the example below. The class vehicles has two variables called manufacturer and color. The methods set_manufacturer, set_color and set_rating is used to update the variables for this class.

We will use the vehicles class as the parent class and define another class called sedan which will be subclass/child class to vehicles as show below.

On line 6 we are calling vehicles.__init__(self) which makes all attributes of vehicles available to the sedan class. We can also use the super builtin() to refer the baseclass.

In the main function, we create an instance of the class sedan as vehObj. We use the set_name and set_speed methods to update the speed and name variables. The output of line of 7 and 8 is given below in Fig 1.

Fig 1: Output of line 7 and 8

Now we use the set_manufacturer, set_color and set_rating methods to update the variables, manufacturer, color and rating of the parent vehicle class. As you can see, we use the vehObj and use the methods inside the parent vehicles class. The output of line 15,16 and 17 is given below in Fig 2.

Fig 2: Output of objects in the vehicles class.

--

--

Abhishek Bose

Machine Learning Engineer II at Swiggy. On a quest for technology.