Abstraction & Abstract Classes in Python
Abstraction means exposing only what a user of a class needs to know — the what — while hiding the how behind the scenes. Python formalizes this with abstract base classes, using the abc module: a class marked as abstract can define methods that every subclass must implement, without providing an implementation itself, and Python prevents the abstract class from being instantiated directly.
Import from the abc module
ABC and abstractmethod provide the tools for building an abstract base class.
Inherit from ABC
Marks the class itself as abstract.
Decorate a method with @abstractmethod
Requires every subclass to implement it, with no body of its own.
The abstract class can’t be instantiated
Attempting it directly raises a TypeError — it exists only to be inherited from.
Subclasses must implement every abstract method
Before they themselves can be instantiated.
from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area(self): pass class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14 * self.radius ** 2 c = Circle(5)print(c.area())Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.