Objects in depth lesson plan
Object-oriented Python past the basics, on a Raspberry Pi: class and instance attributes, inheritance and super(), composition, dunder methods, properties, dataclasses and enum, ending with a device library for a starship.
Objectives and materials
By the end, students can
- What a class really is, and how classes inherit from each other.
- Objects built from objects, and objects that act like Python's own types.
- Properties that guard the hardware, dataclasses and enums that cut the boilerplate.
- A device class library for the starship Kestrel.
Materials for the whole unit
- Half breadboard
- Hobby servo
- LED x 2
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor x 2
- Traffic light module
Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.
Classes and families
What a class really is, and how classes inherit from each other.
| Lesson | Time | What students do |
|---|---|---|
| Classes revisitedLook again at the class you think you know, on the starship Kestrel's lamps: what self really is, the shared-list bug that class attributes cause, the right use for them, classmethods as second constructors, and bound methods. | 40 min |
|
| InheritanceGive the Kestrel's bridge lights a family tree: a Device base class, subclasses that override and extend with super(), one loop that drives any device (polymorphism), and an abstract base class that refuses a subclass missing its methods. | 40 min |
|
Materials
- Half breadboard
- LED x 2
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor x 2
- Traffic light module
Check for understanding
Each student finishes "Inheritance" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "An abstract base class".
Parts and protocols
Objects built from objects, and objects that act like Python's own types.
| Lesson | Time | What students do |
|---|---|---|
| CompositionBuild the Kestrel's bridge panel out of parts instead of a family tree: a 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. | 30 min |
|
| Dunder methodsMake the Kestrel's lamps act like Python's own types: __repr__ and __str__ for printing, __len__, __getitem__, __contains__ and __iter__ for a container, __lt__ and __bool__ for sorting and truth, __add__, and __enter__ and __exit__ for a with block. | 50 min |
|
Materials
- Half breadboard
- LED x 2
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor x 2
- Traffic light module
Check for understanding
Each student finishes "Dunder methods" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "A with block: the lights drill".
Rules and records
Properties that guard the hardware, dataclasses and enums that cut the boilerplate.
| Lesson | Time | What students do |
|---|---|---|
| PropertiesGuard the Kestrel's cargo hatch servo: see a plain attribute lie about where the hatch is, turn it into a property whose setter moves the servo and clamps bad angles, add a read-only computed property, and refuse moves while the hatch is locked. | 50 min |
|
| dataclasses and enumLet Python write the boring parts: a dataclass in place of hand-written __init__, __repr__ and __eq__, frozen records that cannot be changed by accident, an Enum whose typos fail loudly, and the plain-class constants pattern for boards with no enum module. | 40 min |
|
Materials
- Hobby servo
- Pushbutton
- Raspberry Pi 4 Model B
- Traffic light module
Check for understanding
Each student finishes "dataclasses and enum" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Constants without enum".
Project
A device class library for the starship Kestrel.
| Lesson | Time | What students do |
|---|---|---|
| Project: a device class libraryPut the course together as a real library: a kestrel.py module with Device, Lamp and Hatch classes, a Ship that composes them, a serial console that drives any device by name, and a new device type added without touching the code that uses it. | 50 min |
|
Materials
- Half breadboard
- Hobby servo
- LED x 2
- Raspberry Pi 4 Model B
- Resistor x 2
Check for understanding
Each student finishes "Project: a device class library" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Your own device".
Teach this unit as a class
Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.
The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.