
Classes and objects have a central role in Python. Every time you assign a value to a variable, you are unconsciously creating an object. This object belongs to a particular class and owns particular attributes and methods. These characteristics were covered in the previous post, in which I provided a fast overview of classes in Python.
But python classes aren’t only limited to this aspect, but they are also characterized by inheritance. The goal of inheritance is to reuse an already-built class to create a new class. In this way, you don’t always need to create a class from scratch and this class, called Child Class, will inherit the attributes and methods from another class, Parent Class, allowing you to reduce the lines of code and redundancy.
1. Create Parent Class

As explained previously, the Child Class is based on the Parent Class. Hence, the first step is to create the Parent Class. This class will allow creating a Child Class, avoiding writing the same lines of code.
For example, let’s define a medium_user
class, which will collect information like name, surname, username, email, and a boolean attribute "subscriber" to check if the user is a member or not, using the __init__
method. Moreover, it has read
,clap
and is_member
methods.
We create an object using the medium_user
class and we execute all the methods:

Good! We defined a medium user, which didn’t have any subscription on Medium. But in the case, we have a medium member, there are still missing attributes, like the date he started the subscription, the type of membership, and the payment method. To add this information, we’ll need to define the Child class.
2. Create Child Class
Let’s build the medium_subscriber
class, which will acquire the attributes and the methods of the medium_user
class, previously defined. To gradually understand how class inheritance works, we can create an empty class, that simply inherits the functionalities from the other class.
To not add any properties or methods, the pass
keyword is used. We only specify that the medium_subscriber
class is a child of the medium_user
class.

As before, we built a new object that belongs to the medium_subscriber
class. If we try to print an attribute and the methods defined for the medium_user
class, we can surely notice that the medium_subscriber
inherited all the functionality from the Parent Class.
3. Use super() function
In the previous paragraph, we only defined an empty class that inherits attributes and methods from the other class. But we would like to add properties through the __init__
constructor method.
The medium_subscriber
class also needs the super()
function to inherit the functionality from the parent class, otherwise, the child’s constructor method overrides the inheritance of the parent’s __init__
method.
First, let’s try without the super() function to see with our eyes how it would work:

You can observe that the medium_subscriber
class didn’t inherit any property or method from the medium_user
class. We need to add a line of code using the super()
function within the __init__
function.

Now, we properly added the attributes without losing the characteristics of the parent class. First, we say to the medium_subscriber
class to acquire the attributes and methods from the other class and, then, we add the attributes, such as the date he started the subscription, the type of membership, and the payment method.
There is also an alternative to this solution. You can call the parent’s __init__
function, in place of the line with the super()
function. It’s the commented line, medium_user.__init__(self,s_name,s_surname,s_username,s_email)
.The super()
function is usually preferred to avoid writing the name of the parent class.
Final thoughts
This post summarizes well the concept of inheritance in Python classes. It provides reusability, readability, and reduction of redundant code. For these advantages, it’s really widely used to define classes. In my experience, I met it when I defined neural network architectures using Pytorch. You can find an example here where I built a convolutional autoencoder. In that case, the nn.Module
is the parent class that provides all the building blocks to construct the autoencoder class, such as the linear layer, the convolutional layer, the activation functions, and many others. I hope you found useful this post. Thanks for reading. Have a nice day!
Did you like my article? Become a member and get unlimited access to new Data Science posts every day! It’s an indirect way of supporting me without any extra cost to you. If you are already a member, subscribe to get emails whenever I publish new data science and python guides!