Inheritance in Python: Fundamentals for Data Scientists | by Erdem Isbilen | Towards Data Science

Class inheritance is an important concept in object-oriented programming. It enables us to extend the abilities of an existing class. To create a new class, we use an available class as a base and extend its functionality according to our needs. Since we build a new class by using methods and data definitions available in the base class, development & maintenance time reduces and code reusability increases.
This post will introduce you basics of class inheritance in Python.
Let’s write a Python3 code that contains a simple example of class inheritance;
import pandas as pdimport random# BASE CLASSclass CSVGetInfo: """ This class displays the summary of the tabular data contained   in a CSV file """ instance_count = 0  # Initializer / Instance Attributes def __init__(self, path, file_name):  CSVGetInfo.increase_instance_count()  self.path = path  self.file_name = file_name  print("CSVGetInfo class object has been instantiated")  # Instance Method def display_summary(self):  data = pd.read_csv(self.path + self.file_name)  print(self.file_name)  print(data.head(self.generate_random_number(10)))  print(data.info())  return data  # Class Methods @classmethod def increase_instance_count(cls):  cls.instance_count += 1  print(cls.instance_count)  @classmethod def read_file_1(cls):  return cls("/Users/erdemisbilen/Lessons/", "data_by_artists.csv") @classmethod def read_file_2(cls):  return cls("/Users/erdemisbilen/Lessons/", "data_by_genres.csv")  # Static Methods @staticmethod def generate_random_number(limit):  return random.randint(1, limit)# SUB CLASSclass CSVGetColumnDetails(CSVGetInfo): """ This class displays the summary of a column in a tabular data  contained in a CSV file """  # Initializer / Instance Attributes def __init__(self, path, file_name, column_name):  CSVGetInfo.__init__(self, path, file_name)  self.column_name = column_name  print("CSVGetDetail class object has been instantiated")  # Instance Method def display_column_summary(self):  data = self.display_summary()  print(data[self.column_name].describe())  @classmethod def read_file_1(cls, column_name):  return cls("/Users/erdemisbilen/Lessons/", "data_by_artists.csv",   column_name)if __name__ == '__main__': data = CSVGetColumnDetails.read_file_1("danceability") data.display_column_summary()
In our Python code above, we have CSVGetInfo base class which contains several methods and data definitions.
display_summary(self) instance method prints the summary of the tabular data contained in a CVS file by using the values provided in the path, and file_name.
There are also several class methods, read_file_1(cls), read_file_2(cls), and increase_instance_count(cls), to ease the object instantiation of the base class.
Consider that we want to create a new class, CSVGetColumnDetails, to get summary information of a specific column in a tabular data. We can write a new class starting from scratch, but a better way is to inherit and extend some of the methods and data definitions already available in CSVGetInfo class.
# BASE CLASSclass CSVGetInfo: .... ....# SUB CLASSclass CSVGetColumnDetails(CSVGetInfo): .... ....
class SubClassName(BaseClassName) is the Python inheritance syntax used to create a subclass. In our example, our base class is CSVGetInfo and our extended subclass is CSVGetColumnDetails.
We call __init__ method of our base class to instantiate the path and file_name attributes. We derive these attributes from our base class to our subclass.
The column_name attribute that is only available in our subclass level is instantiated with self notation representing CSVGetColumnDetails’s instances.
# Initializer / Instance Attributes def __init__(self, path, file_name, column_name):  CSVGetInfo.__init__(self, path, file_name)  self.column_name = column_name  print("CSVGetDetail class object has been instantiated")
Then we create a new method in our subclass with the help of the display_summary() method derived from the base class. We use and extend the abilities of display_summary() in our subclass to define a new method.
# Instance Method def display_column_summary(self):  data = self.display_summary()  print(data[self.column_name].describe())
This new method in our subclass, display_column_summary(self), displays the summary of a specific column by using the data display_summary (derived from the base class) method returned. This subclass method also has a subclass attribute of column_name.
Note that we have read_file_1 class methods both defined in our base and subclass with the different implementations.
read_file_1 class method in the base class just passes path and file_name values to instantiate the base class objects whereas the one in the subclass instantiates the subclass objects with an additional argument, column_name.
This means that the read_file_1 method in the subclass overrides the same method available in the base class. When these methods are called, they perform their unique implementations.
# BASE CLASSclass CSVGetInfo: .... .... @classmethod def read_file_1(cls):  return cls("/Users/erdemisbilen/Lessons/", "data_by_artists.csv")# SUB CLASSclass CSVGetColumnDetails(CSVGetInfo): .... .... @classmethod def read_file_1(cls, column_name):  return cls("/Users/erdemisbilen/Lessons/", "data_by_artists.csv",   column_name)
There may be some methods or attributes(instance variables) in the base class which are intended to be used only in the base class. They are called as private methods and named with double underbar convention, __privatemethodname. These methods should not be called outside of the base class including subclasses.
There may be also some other methods or attributes in the base class which are intended to be used only in base or subclass definitions. They are called as protected methods and named with single underbar convention, _protectedmethodname. These methods should only be called in base and subclass structures.
Inheritance increases code reusability and reduced development & maintenance time.
Inheritance allows us to define a class that takes and extends all the methods and data definitions available in a base class.
In this post, I explained the basics of class inheritance in Python.
The code in this post and the CSV files used are available in my GitHub repository.
I hope you found this post useful.
Thank you for reading!