liveBook · Manning

Superclass

As explained in Python Workout: 50 ten-minute exercises

A parent class is a class whose attributes and behavior are inherited by a derived child class.

A superclass, or parent class, defines general attributes and behavior that a subclass inherits. This keeps similar classes organized and reduces repeated code. section 9.15

Use a superclass when:

  • Several classes share core behavior but need specialized additions, such as Person and Employee or Vehicle and Car. section 9.15
  • A subclass should reuse or extend parent methods without changing the parent. section 9.16
  • A child initializer needs parent setup; call super().__init__(...) before adding child-specific attributes. section 9.15

A subclass reuses parent behavior

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()) # 20 print(b.x3()) # 30 Python searches an attribute on the instance, its class, then parent classes and object. section 9.10

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

Runnable inheritance example showing a child instance using both inherited and newly defined methods. section 9.10

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains inheritance, ICPO lookup, and parent initialization, but stops short of broader inheritance mechanics.
Examples
Uses Person–Employee, Foo–Bar, Bowl–BigBowl, Envelope–BigEnvelope, and Phone–SmartPhone–iPhone examples.
What it assumes
Assumes familiarity with Python classes, instances, attributes, and methods while teaching inheritance from its basic pattern.
What you can do after
Apply it via Python classes to build subclasses that reuse, extend, or selectively modify parent behavior.
How it connects
Connects superclass to subclass, inheritance, classes, instances, methods, object, and super.

Also explained in

Compare how 10 books explain superclass →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.