Guide 3 · Foundations

Object-Oriented Python

Classes, inheritance, dataclasses, decorators, and an honest look at when a function is the better answer.

Complete 14 notebooks 14 written

Before this guide: Python from the Start.

The notebooks

Open in Colab runs a notebook in your browser, and Read shows it on GitHub without running it. Every notebook runs on its own, and the answers to its exercises are in a separate notebook, linked as Solutions.

1Why ClassesThe problem they solve, and three cases where a function is betterOpen in ColabReadSolutions
2Your First Class__init__, attributes, and self demystifiedOpen in ColabReadSolutions
3MethodsBehavior that belongs to the object, and what self is doingOpen in ColabReadSolutions
4Dunder Methods__repr__, __str__, __eq__, and making your object printableOpen in ColabReadSolutions
5Context Managers and IteratorsWriting the objects behind with and forOpen in ColabReadSolutions
6DecoratorsWhat the @ line does, and writing one of your ownOpen in ColabReadSolutions
7PropertiesComputed attributes, and validation without a getter for everythingOpen in ColabReadSolutions
8Class and Static MethodsAlternative constructors, and methods that need no instanceOpen in ColabReadSolutions
9InheritanceSubclassing, super(), and the method resolution orderOpen in ColabReadSolutions
10Composition over InheritanceThe case against the hierarchy you were about to buildOpen in ColabReadSolutions
11DataclassesMost of the above, for free, in one decoratorOpen in ColabReadSolutions
12InterfacesAbstract base classes, Protocols, and declaring what you requireOpen in ColabReadSolutions
13Exceptions as ClassesYour own exception hierarchy, and what to put in itOpen in ColabReadSolutions
14Worked DesignsShapes side by side with functions, a playlist and notification channels, then a design of your ownOpen in ColabReadSolutions