Python offers different mechanisms to define interfaces and enforce contracts between objects. Two of the most common ones are Abstract Base Classes (ABC) and Protocol.
In this post, we will explore the differences between these mechanisms and how they can be used in Python programming.
Key takeaways
- ABCs are used to define a template or blueprint for a class and enforce a specific interface.
- Protocols are a more lightweight and flexible mechanism for defining interfaces and are often used in dynamic programming and duck typing.
- ABCs and protocols serve different purposes and can be used in different contexts depending on the requirements of the application.
Understanding Abstract Base Classes
An Abstract Base Class (ABC) is a class that defines a set of abstract methods that must be implemented by any class that inherits from it.
In other words, an ABC is a way to define a template or a blueprint for a class and to ensure that any derived class adheres to a specific interface.
ABCs are used to define common functionality across multiple classes and provide a way to check that a class has implemented all the necessary methods before it is used. This makes ABCs useful when you need to define a strict interface for a set of related classes, ensuring that they all share a common set of behaviors.
Here’s an example of an ABC that defines a basic interface for a shape object:
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
In this example, Shape
is an ABC that defines two abstract methods: area()
and perimeter()
. Any class that inherits from Shape
must implement these methods.
Understanding Protocols
A protocol is a more lightweight and flexible mechanism for defining interfaces.
A protocol is simply a set of methods and/or attributes that a class must implement in order to be considered compatible with the protocol.
Protocols can be used to define the expected behavior of objects in a specific context, without requiring the overhead of a full class hierarchy. This makes protocols useful in dynamic programming and duck typing, where the focus is on what an object can do, rather than what it is.
Here’s an example of a protocol that defines a basic interface for a sequence object:
class SequenceProtocol:
def __getitem__(self, index):
raise NotImplementedError
def __len__(self):
raise NotImplementedError
In this example, SequenceProtocol
is a protocol that defines two methods: __getitem__()
and __len__()
. Any class that implements these methods can be considered compatible with the SequenceProtocol
.
ABC vs Protocol
While both ABCs and protocols are used to define interfaces and enforce contracts between objects, they serve different purposes and can be used in different contexts.
- ABCs are useful when you need to define a strict interface for a set of related classes, ensuring that they all share a common set of behaviors.
- Protocols are useful when you need to define a more flexible interface that can be implemented by any class that provides the necessary methods and attributes.
Conclusion
In general, ABCs are more heavyweight and require a more formal definition of the interface, while protocols are more lightweight and can be defined more informally. The choice between ABCs and protocols will depend on the requirements of your application and the level of interface definition that is needed.