Python abstraction with abstract classes
Specify required behavior while leaving implementation to subclasses.
On this page
Inheritance and methods.
- Specify required behavior while leaving implementation to subclasses.
- Predict the sample output and explain the state changes.
- Complete the practice task and check the boundary cases.
The concept
An abstract base class can declare methods that concrete subclasses must implement. It describes what callers may request without prescribing one implementation.
Shape requires area. Rectangle supplies it using width and height. Instantiating Shape itself would raise TypeError because its abstract method is not implemented.
Code example
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() == 012
Follow the code
Open the walkthrough to inspect the example one step at a time.
Show guided walkthrough
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() == 0Follow the example
Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.
View the complete dry-run table
| Step / line | What happens | State after step | Output so far |
|---|---|---|---|
| Define the abstract contract. | — | — | |
| Create a concrete instance. | width = 3, height = 4 | — | |
| Compute the rectangle implementation of area. | return = 12 | — | |
| Print the result. | return = 12 | 12 | |
| Check a zero-width rectangle. | return = 12 | 12 |
Behind the scenes
How Python processes this code
Python runs this source through its interpreter. It creates objects for values and resolves names as each statement runs.
Cost and common mistakes
O(1) arithmetic operations and O(1) object state.
- This example assumes nonnegative numeric dimensions.
- An abstract base class is useful for an explicit contract, but is not required for all Python polymorphism.
Try it yourself
Implement Square with one side and an area method.
Reveal answer and reasoning
Store side in __init__, return side * side, and check that Square(5).area() is 25.