@dataclass — the class shape AI ships in every modern Python project — step 9 of 9
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'])
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'])
this step needs the editor
on desktop today; in the app (coming soon). save your spot and we'll bring you back here when you're ready.