When several classes need the same attributes and behavior, copying code into each one makes mistakes harder to find and fix. Inheritance offers another route: put shared features in a parent class, then derive child classes that add or change what they need. This supports the DRY (“don’t repeat yourself”) principle without making every child identical. section 4.1.3
A parent class is also called a base class or superclass; a child is a derived class or subclass. In Python, `class ChildClass(ParentClass):` declares the relationship. The child inherits the parent’s attributes and behavior and can define features of its own. section 4.1.3 section 4.3
A family of drawn shapes
Start with a `Shape` class for features shared by shapes on a screen. `Rectangle(Shape)` supplies rectangle-specific drawing. `Square(Rectangle)` uses `super().__init__()` to initialize its parent with equal width and height, while `Circle(Shape)` supplies its own drawing method. The hierarchy reuses common features without requiring every shape to be drawn the same way. section 4.1.3
class Square(Rectangle):
def __init__(
self,
x: int,
y: int,
size: int,
pen_color: tuple = COLOR_PALETTE[0],
fill_color: tuple = COLOR_PALETTE[1],
dir_x: int = 1,
dir_y: int = 1,
speed_x: int = 1,
speed_y: int = 1,
):
super().__init__(
x, y,
size,
size,
pen_color,
fill_color,
dir_x,
dir_y,
speed_x,
speed_y
)
The `Square` constructor shows a practical child-class pattern: accept a size, then pass it twice to the `Rectangle` initializer through `super().__init__()`. section 4.1.3
Use inheritance when the parent–child relationship makes sense, not merely because two classes need some of the same features. Composition can add attributes and behavior without creating an awkward hierarchy. The word also appears in Jinja2 templates: a page-specific template can inherit common website elements from another template, reducing repeated page construction. section 4.3 section 6.7


















