Skip to content
Search
Generic filters
Exact matches only

Abstract Base Classes in Python: Fundamentals for Data Scientists

Understand the basics with a concrete example!

Erdem Isbilen
Photo by Joshua Aragon on Unsplash

In Python, abstract base classes provide a blueprint for concrete classes. They don’t contain implementation. Instead, they provide an interface and make sure that derived concrete classes are properly implemented.

  • Abstract base classes cannot be instantiated. Instead, they are inherited and extended by the concrete subclasses.
  • Subclasses derived from a specific abstract base class must implement the methods and properties provided in that abstract base class. Otherwise, an error is raised during the object instantiation.

Let’s write a Python3 code that contains simple examples of implementing abstract base classes:

from abc import ABCMeta, abstractmethod
Photo by John Duncan on Unsplash

Python provides an abc module for the abstract base class definition. We need to import ABCMeta metaclass and assign it to the abstract class we want to define.

from abc import ABCMeta, abstractmethod

Inside the abstract base class, we force the subclasses to implement the path and file_name properties with the use of @abstractmethod decorator. Note that the properties are not implemented and empty as abstract classes are used only for defining the interface.

 @property
@abstractmethod

def path(self):
pass

Again, with @abstractmethod decorator, we can define an abstract method and force the subclasses to implement the display_summary(self) method.

 @abstractmethod
def display_summary(self):
pass

At this point, if we try to instantiate an object of AbstactClassCSV abstract class, we get an error.

abc_instantiation = AbstactClassCSV("/Users/erdemisbilen/Lessons/", "data_by_genres.csv")
Photo by Benoit Gauzere on Unsplash

Now that we have our abstract base class (AbstactClassCSV) is defined, we can create our subclass by inheriting.

Below, I created CSVGetInfo concrete class, by inheriting from AbstactClassCSV abstract class. Therefore, I must strictly follow the interface provided by the abstract class and implement all dictated methods and properties properly in my concrete subclass.

class CSVGetInfo(AbstactClassCSV):

As to demonstrate the case, I only defined the path property and leave the file_name property and display_summary method not implemented.

If I try to instantiate an object of CSVGetInfo in such state, the above error is raised.

That is how abstract classes prevent improper subclass definition.

class CSVGetInfo(AbstactClassCSV):
""" This class displays the summary of the tabular data contained
in a CSV file """

As we defined all required properties and the methods dictated by abstract class above, now we can instantiate and use the objects of derived subclass CSVGetInfo.

Photo by Vincent van Zalinge on Unsplash
  • Abstract base classes separate the interface from the implementation.
  • They make sure that derived classes implement methods and properties dictated in the abstract base class.
  • Abstract base classes separate the interface from the implementation. They define generic methods and properties that must be used in subclasses. Implementation is handled by the concrete subclasses where we can create objects that can handle tasks.
  • They help to avoid bugs and make the class hierarchies easier to maintain by providing a strict recipe to follow for creating subclasses.

In this post, I explained the basics of abstract base classes in Python.

The code in this post is available in my GitHub repository.

I hope you found this post useful.

Thank you for reading!