This module introduces object modeling with dataclass-style structures and classes.
- Difficulty: Intermediate.
- Estimated Time: 35-50 minutes.
- Prerequisites:
01-foundations/functions,01-foundations/scope-and-lifetime-basics. - Cross-Language Lens: Compare lightweight records, full classes, and method receivers to see how each language models data plus behavior.
python example/main.py- Lightweight value modeling with
@dataclass. - Stateful behavior using classes and private-by-convention fields.
- Constructor guards to preserve valid object state.
- Method-based updates for controlled mutations.
- Placing mutable shared defaults directly on class definitions.
- Letting invalid constructor inputs propagate through the object lifecycle.
- Updating internal state from outside helper methods.
- exercises/01.py: model a rectangle using a class with methods.
- exercises/02.py: build an encapsulated
Counterclass with commands.
- exercises/01.py
- Input: width and height.
- Output: area and perimeter.
- Edge cases: non-positive dimensions should stop with a message; decimal values should work.
- exercises/02.py
- Input: sequence of commands (
inc,dec,reset,stop). - Output: counter value updates and final value.
- Edge cases: unknown commands; immediate
stop.
- I can explain when
@dataclassis enough and when a class with methods is better. - I can enforce simple invariants in constructors.
- I can keep state updates inside well-named methods.
- I completed exercises/01.py.
- I completed exercises/02.py.