Python > Object-Oriented Programming (OOP) in Python > Inheritance > Abstract Base Classes (ABCs) with `abc` module
Abstract Base Classes (ABCs) and Inheritance in Python
This snippet demonstrates how to use Abstract Base Classes (ABCs) with inheritance in Python using the abc
module. ABCs enforce that derived classes implement specific methods, ensuring a consistent interface. This promotes code robustness and maintainability, particularly in large projects or when working with multiple developers.
Core Concept: Abstract Base Classes
Abstract Base Classes (ABCs) define a blueprint for other classes. They cannot be instantiated directly but serve as a template. An ABC declares abstract methods that derived classes must implement. This ensures that all subclasses adhere to a certain interface. Python's abc
module provides the necessary tools to create ABCs.
Code Example: Shape Hierarchy with ABC
This example defines an abstract base class Shape
with abstract methods area
and perimeter
. The Rectangle
and Circle
classes inherit from Shape
and provide concrete implementations for these methods. If a subclass fails to implement the abstract methods, Python will raise a TypeError
during instantiation.
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def perimeter(self):
return 2 * (self.width + self.height)
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14159 * self.radius * self.radius
def perimeter(self):
return 2 * 3.14159 * self.radius
# Attempting to create an incomplete class will raise an error
# class IncompleteShape(Shape):
# pass
# incomplete = IncompleteShape()
rectangle = Rectangle(5, 10)
circle = Circle(7)
print(f"Rectangle Area: {rectangle.area()}")
print(f"Rectangle Perimeter: {rectangle.perimeter()}")
print(f"Circle Area: {circle.area()}")
print(f"Circle Perimeter: {circle.perimeter()}")
Explanation of the abc
module
The abc
module provides infrastructure for defining abstract base classes (ABCs) in Python. The core components are:
Using these tools, you can enforce a specific interface in your class hierarchy.ABC
to become an abstract base class.
Real-Life Use Case: Plugin Systems
ABCs are excellent for building plugin systems. Imagine a software application that allows users to extend its functionality with plugins. You can define an ABC that represents the interface a plugin must implement. All plugin developers must then adhere to this interface, ensuring compatibility and predictability. For example, a text editor might define an AbstractPlugin
with methods like execute(text)
. All plugins must implement this method to be usable within the editor.
Best Practices
@abstractproperty
: If you need to enforce the existence of properties, use @abstractproperty
(available in some versions of Python).
Interview Tip
When discussing ABCs in an interview, emphasize their role in enforcing interfaces and promoting code reusability and maintainability. Be prepared to explain how they differ from regular classes and interfaces in other languages. Mention use cases like plugin systems or framework design.
When to Use ABCs
Use ABCs when:
Memory Footprint
ABCs themselves don't significantly impact memory footprint. The memory used is primarily for storing the class definition and the abstract method declarations. The real memory consumption comes from the instantiated objects of the concrete subclasses.
Alternatives
Pros of Using ABCs
Cons of Using ABCs
FAQ
-
Can I instantiate an Abstract Base Class?
No, you cannot directly instantiate an Abstract Base Class. Attempting to do so will raise aTypeError
. ABCs are meant to be inherited from and their abstract methods implemented by concrete subclasses. -
What happens if a subclass doesn't implement all abstract methods?
If a subclass does not implement all abstract methods defined in the ABC, you will get aTypeError
when you try to instantiate the subclass. This enforces the contract defined by the ABC. -
Can I have concrete methods in an Abstract Base Class?
Yes, ABCs can contain both abstract and concrete methods. Concrete methods provide default implementations that subclasses can either use or override.