Types and contracts
What are abstract classes?
On this page
An abstract class defines a common base that may combine reusable code with operations subclasses must provide. A template importer can supply the read-and-validate sequence while subclasses parse different formats. Put a step in the base only when every supported importer shares its ordering and failure semantics.
Worked example
Pythonfrom abc import ABC, abstractmethod
class Importer(ABC):
def load(self, raw):
return self.parse(raw.strip())
@abstractmethod
def parse(self, raw):
pass
class CountImporter(Importer):
def parse(self, raw):
return int(raw)
assert CountImporter().load(" 12 ") == 12Explore class relationships in the OOP Playground