Skip to content
UPEEC

Abstract Base Classes

abc.ABC and @abstractmethod define interfaces that subclasses must implement.

Intermediate
Comments 0
from abc import ABC, abstractmethod
class Sensor(ABC):
    @abstractmethod
    def read(self) -> float: ...

Sensor()   # TypeError: can't instantiate abstract class

typing.Protocol offers a structural ("duck-typed") alternative checked by type checkers.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.