Last Updated: January 3, 2026
The abc module in Python is a powerful tool for defining abstract base classes. If you've been working with abstract classes, you'll find the abc module helps to enforce a contract for subclasses while also providing a clearer, more manageable code structure.
This chapter will delve deep into the abc module, exploring its features, practical applications, and how it fits into the broader concepts of polymorphism and abstraction.
At its core, the abc module allows us to define abstract base classes (ABCs) that cannot be instantiated directly. Instead, they serve as blueprints for other classes. You can think of an abstract base class as a set of guidelines that ensure derived classes implement specific methods.
The abc module is part of the standard library, so you don’t need to install anything extra. Just import it, and you’re ready to start defining your abstract classes.
Let’s get familiar with its main components:
In this example, Shape is an abstract base class with two abstract methods: area and perimeter. Any class that derives from Shape must implement these methods, or it cannot be instantiated.
Abstract methods are a core feature of the abc module. They are methods that must be implemented by any subclass. If a subclass fails to implement an abstract method, Python will raise a TypeError when you try to create an instance of that subclass.
Here’s a closer look at how we can use abstract methods:
In this Circle class, we’ve implemented both abstract methods defined in the Shape class. Now, we can create instances of Circle:
You can also define abstract properties using the @property decorator along with @abstractmethod. This ensures that subclasses must implement these properties:
In this case, area and perimeter are properties rather than methods. This allows for a more natural way of accessing these values without needing to call them as methods.
Once you have your abstract base class defined, you can create concrete implementations. Any subclass that implements all the abstract methods and properties can be instantiated.
Let’s expand our example to include a Triangle class:
Now we can create instances of Circle, Rectangle, and Triangle:
This polymorphic behavior is one of the key benefits of using the abc module. It allows you to interact with different shapes through a common interface defined by the abstract base class.
Using the abc module comes with several benefits that enhance the overall design and maintainability of your code:
abc module ensures that certain functionalities are always present.While the abc module is powerful, it does come with some pitfalls that developers often encounter:
One common mistake is to forget to implement all abstract methods in a subclass. This will lead to runtime errors when trying to instantiate the subclass. Always ensure that you’re covering all abstract methods.
Attempting to create an instance of an abstract base class will raise a TypeError.
@abstractmethod CorrectlyEnsure that you are using the @abstractmethod decorator correctly. If you forget to add it to a method in an abstract base class, that method will not be enforced in subclasses.
It’s perfectly valid to have concrete methods alongside abstract methods in your ABC. However, be mindful that if you create a concrete method that relies on an abstract method, it could lead to confusion if the abstract method isn’t implemented in the subclass.
The abc module is particularly useful in scenarios where you want to define a set of behaviors for a group of related classes:
Now that you understand how to leverage the abc module to define abstract base classes and enforce a contract for your subclasses, you’re ready to explore the next layer of abstraction with interfaces and protocols.
In the next chapter, we will look at how interfaces and protocols can enhance your design flexibility and promote even more dynamic interactions in your Python programs, building on the foundation we’ve established with the abc module.