Python abstraction with abstract classes

An abstract base class can declare methods that concrete subclasses must implement. It describes what callers may request without prescribing one implementation.

Python
from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def area(self):
        return self.width * self.height

shape = Rectangle(3, 4)
print(shape.area())
assert Rectangle(0, 4).area() == 0
Read the full lesson