classes — reading what ai just wrote you9 / 9
@dataclass — the class shape ai ships in every modern python project
Checkpoint
One last thing before we move on. Same surface as a write step — but the lesson doesn't complete until this passes.
Last drill. Build two dataclasses that fit together:
-
Address— afrozen=Truedataclass with two fields:street: strcity: str
-
Customer— a regular@dataclasswith three fields:name: straddress: Address(a nested dataclass — required)orders: listdefaulting tofield(default_factory=list)
Then create two customers sharing the same address (an Address is
immutable, so sharing is fine), append an order to each, and print
both.
Expected output:
Customer(name='maya', address=Address(street='1 main st', city='oak'), orders=['#1'])
Customer(name='marcus', address=Address(street='1 main st', city='oak'), orders=['#2'])
⌘↵ runs the editor.
Booting Python…
Output
[promptdojo:~]$ _