liveBook · Manning

Inheritance

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

Inheritance is an object-oriented programming mechanism in which a class derives attributes and behavior from another class.

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

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Explains parent–child terminology and Python declaration syntax, then develops constructors and drawing methods through a shape hierarchy.
Examples
Builds on screen-drawn rectangles, squares, and circles, including a square initialized through its rectangle parent.
What it assumes
Builds on familiarity with Python classes and objects while introducing parent–child class syntax explicitly.
What you can do after
Build it from scratch by defining Python subclasses that reuse parent initialization and add shape-specific behavior.
How it connects
Connects inheritance to classes, encapsulation, DRY, polymorphism, and composition.

Also explained in

Compare how 19 books explain inheritance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.