liveBook · Manning

Inheritance

As explained in Python Workout, Second Edition

Inheritance is the Python class relationship in which a derived class receives attributes and behavior from one or more base classes.

Inheritance lets a subclass reuse and extend a parent class. Use it when the relationship is “is-a”: an Employee is a Person, a Car is a Vehicle, or a BigBowl is a Bowl. Do not confuse this with composition, where one object contains another: a Bowl has-a Scoop. section 10.10 section 10.16

Define the parent class in parentheses after the subclass name. Python searches for attributes through the instance, its class, the class’s parents, and ultimately object. A subclass therefore receives inherited methods and can replace a method by defining an attribute with the same name. section 10.12 section 10.6

Extend a class with minimal code

A Bowl can enforce a three-scoop limit while BigBowl changes only the capacity. Store the limit as a class attribute and let the inherited method read self.max_scoops. The same method then uses 3 for Bowl instances and 5 for BigBowl instances. section 10.17 section 10.18

class Foo():
    def __init__(self, x):
        self.x = x
    def x2(self):
        return self.x * 2


class Bar(Foo):
    def x3(self):
        return self.x * 3


b = Bar(10)

print(b.x2())       #1
print(b.x3())     #2

Shows a Bar instance receiving __init__ and x2 from Foo while defining its own x3 method. section 10.12

When a subclass defines its own __init__, call super().__init__ to reuse the parent initialization, then assign subclass-specific attributes. This avoids duplicating parent setup and keeps the hierarchy easier to maintain. section 10.16 section 10.26

Practice inheritance by deciding what belongs in the shared parent and what belongs in each subclass:

  • Create Animal with shared representation and initialization, then derive Wolf, Sheep, Snake, and Parrot with species-specific details. section 10.25 section 10.26
  • Extend dict with FlexibleDict, which retries missing keys after converting them to str and int. section 10.21
  • Design Envelope and BigEnvelope, or Phone, SmartPhone, and iPhone, to practice inherited behavior and selective overrides. section 10.20

Inheritance reduces repeated code and clarifies relationships among similar types, but excessive inheritance can create unwieldy designs. Compare the is-a relationship with composition before choosing the technique. section 10.2

How Python Workout, Second Edition covers it

How deep it goes
Traces attribute lookup through instances, classes, parents, and object, then applies inheritance through exercises and overrides.
Examples
Uses Person and Employee, Foo and Bar, Bowl and BigBowl, Animal subclasses, and dict extensions as implementation exercises.
What it assumes
Assumes familiarity with Python classes, methods, attributes, and object creation while practicing inheritance alongside composition.
What you can do after
Build it from scratch by defining subclasses, reusing parent initialization with super, overriding methods, and extending built-in classes.
How it connects
Connects inheritance with classes, subclasses, composition, attributes, method overriding, super, built-in types, and object-oriented programming.

Also explained in

Compare how 19 books explain inheritance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.