Lesson plan

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.

4 chapters, 7 lessons, about 5 hours, on the Raspberry Pi 4 Model B. Every lesson runs in the browser, with nothing to install.

The unit

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.

Chapter 1. About 1 h 20 min

Classes and families

What a class really is, and how classes inherit from each other.

LessonTimeWhat 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
  1. Run it: the Kestrel's lamps
  2. The shared list bug
  3. Per object, per class
  4. A classmethod constructor
  5. Bound methods
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
  1. Run it: a family of devices
  2. Override: a blinking lamp
  3. Extend with super()
  4. One loop, any device
  5. An abstract base class

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".

Chapter 2. About 1 h 20 min

Parts and protocols

Objects built from objects, and objects that act like Python's own types.

LessonTimeWhat 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
  1. Run it: a panel that has lamps
  2. Modes as data
  3. Hand the parts in
  4. A button in the panel
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
  1. Run it: an object that prints badly
  2. __repr__ and __str__
  3. A container
  4. Sorting and truth
  5. Adding to a bank
  6. A with block: the lights drill

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".

Chapter 3. About 1 h 30 min

Rules and records

Properties that guard the hardware, dataclasses and enums that cut the boilerplate.

LessonTimeWhat 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
  1. Run it: the cargo hatch
  2. An attribute that lies
  3. A property that moves the servo
  4. Validation in the setter
  5. A read-only property
  6. A safety interlock
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
  1. Run it: boilerplate and a silent typo
  2. A dataclass
  3. Frozen records
  4. An Enum for the modes
  5. Constants without enum

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".

Chapter 4. About 50 min

Project

A device class library for the starship Kestrel.

LessonTimeWhat 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
  1. Run it: a library in its own module
  2. Add the hatch
  3. A ship that has devices
  4. A console for any device
  5. A new device, no other changes
  6. Your own device

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".

Your class

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.