How do you specify and enforce an interface spec in Python?

An interface specification defines the contract that classes must follow, describing method signatures and behavior. Python provides several ways to specify and enforce interfaces using Abstract Base Classes (ABCs) and testing approaches.

Using Abstract Base Classes (abc module)

The abc module allows you to define Abstract Base Classes that enforce interface contracts ?

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)

# Create instance
rect = Rectangle(5, 3)
print(f"Area: {rect.area()}")
print(f"Perimeter: {rect.perimeter()}")
Area: 15
Perimeter: 16

Enforcing Interface Implementation

Abstract classes cannot be instantiated directly and require subclasses to implement all abstract methods ?

from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class IncompleteShape(Shape):
    pass

# This will raise TypeError
try:
    incomplete = IncompleteShape()
except TypeError as e:
    print(f"Error: {e}")
Error: Can't instantiate abstract class IncompleteShape with abstract method area

Using Protocol Classes (Python 3.8+)

Protocol classes provide structural subtyping for more flexible interface definitions ?

from typing import Protocol

class Drawable(Protocol):
    def draw(self) -> str:
        ...

class Circle:
    def draw(self) -> str:
        return "Drawing a circle"

class Square:
    def draw(self) -> str:
        return "Drawing a square"

def render_shape(shape: Drawable) -> str:
    return shape.draw()

# Both classes satisfy the Drawable protocol
circle = Circle()
square = Square()

print(render_shape(circle))
print(render_shape(square))
Drawing a circle
Drawing a square

Built-in Abstract Base Classes

Python's collections.abc module provides ready-to-use ABCs for common interfaces ?

from collections.abc import Iterable, Container

class NumberContainer:
    def __init__(self, numbers):
        self.numbers = numbers
    
    def __iter__(self):
        return iter(self.numbers)
    
    def __contains__(self, item):
        return item in self.numbers

container = NumberContainer([1, 2, 3, 4, 5])

# Check if it implements the interfaces
print(f"Is Iterable: {isinstance(container, Iterable)}")
print(f"Is Container: {isinstance(container, Container)}")

# Use the interfaces
print(f"Contains 3: {3 in container}")
for num in container:
    if num > 3:
        print(f"Found: {num}")
Is Iterable: True
Is Container: True
Contains 3: True
Found: 4
Found: 5

Testing Interface Implementation

Use isinstance() and issubclass() to verify interface compliance ?

from abc import ABC, abstractmethod

class Serializable(ABC):
    @abstractmethod
    def serialize(self):
        pass

class User(Serializable):
    def __init__(self, name):
        self.name = name
    
    def serialize(self):
        return {"name": self.name}

user = User("Alice")

# Check interface compliance
print(f"User implements Serializable: {isinstance(user, Serializable)}")
print(f"User class is subclass: {issubclass(User, Serializable)}")
print(f"Serialized: {user.serialize()}")
User implements Serializable: True
User class is subclass: True
Serialized: {'name': 'Alice'}

Comparison of Interface Approaches

Approach Python Version Type Checking Runtime Enforcement
ABC (abc module) 2.6+ Limited Yes
Protocol Classes 3.8+ Yes (mypy) No
Duck Typing All versions No No

Conclusion

Use Abstract Base Classes for strict runtime interface enforcement, Protocol classes for flexible type checking, or built-in ABCs from collections.abc for standard interfaces. Choose the approach that best fits your application's needs and Python version requirements.

Updated on: 2026-03-26T21:50:18+05:30

229 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements