Abstract Base Classes — ELI5
Imagine you run a restaurant chain. Every location must serve the same menu items — burgers, fries, drinks. You don’t care how each kitchen makes them, but the items must exist on every menu.
That’s what an Abstract Base Class does. It’s a checklist for code.
You write a master blueprint that says: “Any class that calls itself a PaymentProcessor must have a charge() method and a refund() method.” If a developer creates a new payment class but forgets refund(), Python raises an error immediately — before the code ever runs in production.
Without ABCs, you’d only discover the missing method when a customer tries to get a refund and the whole system crashes. That’s like finding out the fire exit is bricked up during an actual fire.
The key word is abstract. An abstract method is a promise: “I exist, but I don’t do anything yet — you fill in the details.” The base class itself is never used directly. It’s the blueprint, not the building.
Regular classes in Python are flexible — you can add or remove any method you want. ABCs trade some of that freedom for safety. They enforce a contract: “If you want to be part of this family, here’s what you must bring.”
One thing to remember: ABCs catch mistakes at class creation time, not at runtime. They’re your early warning system — like spell-check for your code’s structure.
See Also
- Python Class Decorators Understand Class Decorators through an everyday analogy so Python behavior feels intuitive, not random.
- Python Composition Vs Inheritance Understand Composition Vs Inheritance through an everyday analogy so Python behavior feels intuitive, not random.
- Python Cooperative Multiple Inheritance Why Python classes can have multiple parents and still get along — like a kid learning different skills from each family member.
- Python Dataclasses Advanced Understand Dataclasses Advanced through an everyday analogy so Python behavior feels intuitive, not random.
- Python Descriptors Understand Descriptors through an everyday analogy so Python behavior feels intuitive, not random.