📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero Abstract Classes

Abstract Classes

6 min read
Abstract Base Classes define required methods that subclasses must implement. Inherit from ABC and mark methods with @abstractmethod. Python raises TypeError if you instantiate a class with abstract methods unimplemented.

Abstract Base Classes

from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self) -> float: ...

    @abstractmethod
    def perimeter(self) -> float: ...

class Circle(Shape):
    def __init__(self, r): self.r = r
    def area(self): return 3.14159 * self.r**2
    def perimeter(self): return 2 * 3.14159 * self.r

c = Circle(5)
print(c.area())  # 78.54