13.9. Subclassing and Derivation

Composition works fine when classes are distinct and are a required component of larger classes, but when you desire “the same class but with some tweaking,” derivation is a more logical option.

One of the more powerful aspects of OOP is the ability to take an already-defined class and extend it or make modifications to it without affecting other pieces of code in the system that use the currently-existing classes. OOD allows for class features to be “inherited” by “descendant” classes or “subclasses.” These subclasses “derive” the core of their attributes from “base” (a.k.a. ancestor, super) classes. In addition, this derivation may be extended for multiple generations. Classes involved in a one-level derivation ...

Get Core Python Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.