Objects & classes: state and behaviour move in together
420-302-VA · WEEK 6 · FALL 2026

Utility · sources and further reading

Resources

Everything below is the canonical version of what this hub teaches. One heads-up: Automate the Boring Stuff, this course's usual second telling, deliberately skips classes, so for OOP the official tutorial is both the primary and the gentle path. Same rule as every week: when a random tutorial disagrees with these, these win.

Reference documents

DocumentUse it forNature
Python Tutorial: ClassesThis week's core reading. Sections 9.1 to 9.4 cover everything the hub uses; inheritance (9.5) is worth a skim for the reading rules; the rest can wait for a later courseOfficial (PSF)
Python Tutorial: Data StructuresLists, properly: section 5.1 is the full version of the "just enough lists" asideOfficial (PSF)
PEP 8, the style guideThe naming conventions (CapWords classes, lowercase instances) and layout rules the whole Python world shares; skim the naming section now, return for the rest laterOfficial (PSF)
gpiozero: output devices · input devicesReal class documentation to practise reading: constructors, attributes, methods, and the "Bases:" lines the inheritance section decodedProject docs
Object-oriented programmingThe concepts and their history, Simula's simulation origins included, and the vocabulary (encapsulation, inheritance, polymorphism) in one placeEncyclopedic
Class (computer programming) · Composition over inheritanceThe class idea across languages, and the professional argument behind this course's write-has-a adviceEncyclopedic

Background reading, matched to the stages

StageRead this for the ideas behind it
Objects you ownTutorial 9.1 to 9.3 (scopes can be skimmed; the objects part read closely); the OOP article's history section for why simulation birthed the paradigm
First classTutorial 9.3 to 9.4 (class and instance variables, exactly today's material); Data Structures 5.1 for the list methods; PEP 8 naming
State & devicesThe FSM article from Week 5, reread with attributes in mind; composition over inheritance for the has-a/is-a advice, argued at length
Build labTutorial chapter 6 (Modules), first two sections, for the machinery behind your import; gpiozero's class pages as the model your docstrings imitate

Practice

  • Paper class katas. Spec then write, no computer: Door (open/close/is_open), Thermostat (setpoint, current, needs_heating()), Counter (increment, reset, value), TrafficLight (three states, next()). One per day is midterm gold.
  • Refactor your history. Any Week 4 ladder script rebuilt as a class is a full workout: find the state, name the owner, extract the methods, verify behaviour unchanged.
  • Trace method calls. Take ReactionLog, invent three calls, trace them with a self column, then check in the REPL. The gap between table and interpreter is the study list.
  • Read one real class end to end. gpiozero's Button page: identify constructor arguments, attributes vs methods, what is inherited from where. Fifteen minutes, and library documentation stops being weather.
  • Midterm drills. The review plan is the schedule; the hubs' checklists and quizzes are the question bank.

Glossary

TermMeaning
ClassThe blueprint: attributes and methods every instance of this kind has.
Instance / objectOne concrete thing built from a class, with its own state.
AttributeState on an instance, reached by dot, no parentheses.
MethodBehaviour on a class, called through an instance, with parentheses.
Constructor / __init__The method run at creation to set starting state; "dunder init".
selfInside a method, the instance being operated on; supplied by Python, listed by you.
Bound methodA method tied to its instance, passable as a value; what callbacks receive.
Class attributeA name assigned in the class body, attached to the blueprint and seen by all instances.
Instance attributeA name created through self, owned by one instance; the default for state.
EncapsulationState changed only through the methods that know its rules.
Composition (has-a)Building objects that contain other objects as attributes.
Inheritance (is-a)Defining a class as a specialization of another; the "Bases:" lines.
DocstringThe string first line of a def or class; what help() shows.
ModuleA .py file importable by its name; today, yours.
RefactoringChanging structure while preserving behaviour, proven by re-run checks.
DunderDouble-underscore names like __init__: Python's hooks into your class.

← Troubleshoot · Start here · Week 5 hub · Week 4 hub