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.
- Classes revisited: what
selfis, 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. - Inheritance: a
Devicebase class, a blinking lamp that overrideson, an alert lamp that extends it withsuper(), one loop that reports any device, and an abstract base class that refuses a device with nostatus(). - 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.
- Dunder methods:
__repr__and__str__, a lamp bank withlen,in, square brackets andfor, sorting by power with__lt__and__gt__,__bool__,__add__, and a lights drill in awithblock. - 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. - dataclasses and enum: a dataclass in place of twelve lines of
boilerplate, frozen records, an
Enumwhose typos fail loudly, and the plain-class constants pattern for boards with noenum. - 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.
maxasksa > band does not try the mirrorb < a, so a class sorted withmaxneeds__gt__as well as__lt__.enumanddataclassesare here. MicroPython drops class annotations, so they read the class from its.pytab, which works for any class in the project. On a microcontroller board there is usually noenum, 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 ininput(); the console's beacon flashes withsleep()instead. - A bound method prints as
<bound_method>, there is no__mro__orvars(), 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.