Lessons and the learning game

Objects in depth

Object-oriented Python past the basics on a Pi: every device on the starship Kestrel becomes a class, ending in a device library and console.

The course

Objects in depth is an intermediate Coding course in seven lessons, for anyone who can already write a class and wants to know what makes fifty of them stay easy to change. Every lesson runs on a gallery Raspberry Pi 4 build: two LEDs on GPIO17 and GPIO27, the traffic light module with a button, or a servo on GPIO18. Each step is checked against what the Pi prints, which pin it drives or where the servo points.

  1. Classes revisited: what self is, the shared-list bug a class attribute causes, the right use for one (a counter), a classmethod that builds a lamp from a header pin number, and bound methods.
  2. Inheritance: a Device base class, a blinking lamp that overrides on, an alert lamp that extends it with super(), one loop that reports any device, and an abstract base class that refuses a device with no status().
  3. Composition: a bridge panel that has lamps, modes as data, lamps handed in from outside so a fake can stand in for testing, and a button wired to a bound method.
  4. Dunder methods: __repr__ and __str__, a lamp bank with len, in, square brackets and for, sorting by power with __lt__ and __gt__, __bool__, __add__, and a lights drill in a with block.
  5. Properties: the cargo hatch servo, an attribute that lies about where the hatch is, a property whose setter moves the servo and clamps bad angles, a read-only is_open, and a safety interlock.
  6. dataclasses and enum: a dataclass in place of twelve lines of boilerplate, frozen records, an Enum whose typos fail loudly, and the plain-class constants pattern for boards with no enum.
  7. Project: a device class library: the Kestrel's library.

The finale

The project moves the classes into a module, kestrel.py, beside a short main.py. Device, Lamp and Hatch share a base class, a Ship composes them, and a serial console drives any device by name:

kestrel> close hatch
hatch -> closed
kestrel> on beacon
beacon -> steady after 3 flashes

The last checked step adds a Beacon without touching Ship or the console, the test of a good class design. Then the learner adds a device of their own and explains the library.

What the Pi's engine does differently

The Pi runs MicroPython 1.29, not CPython. For this course that shows in a few places:

  • A dict does not keep the order keys went in, so the lamp bank keeps a list and the ship's report sorts its names.
  • max asks a > b and does not try the mirror b < a, so a class sorted with max needs __gt__ as well as __lt__.
  • enum and dataclasses are here. MicroPython drops class annotations, so they read the class from its .py tab, which works for any class in the project. On a microcontroller board there is usually no enum, which is why the course also teaches plain-class constants.
  • There are no threads. gpiozero's blink() runs between the program's own steps, so it stops while a program waits in input(); the console's beacon flashes with sleep() instead.
  • A bound method prints as <bound_method>, there is no __mro__ or vars(), and some error messages are worded differently, as each lesson notes.

Everything else, from super() to @property and abc, runs the same on a real Pi.