Check your understanding of the previous reading: chained attribute access on nested dataclasses, the order of definitions, and temporary variables.
Given the following code…
from dataclasses import dataclass
@dataclass
class Location:
city: str
state: str
@dataclass
class Trip:
origin: Location
destination: Location
duration: int
my_trip = Trip(Location("Newark", "DE"),
Location("Baltimore", "MD"),
90)
What will each of the following expressions produce?
Given the following code:
from dataclasses import dataclass
@dataclass
class Position:
x: int
y: int
@dataclass
class Rectangle:
start: Position
end: Position
color: str
@dataclass
class Button:
box: Rectangle
text: str
@dataclass
class World:
save: Button
load: Button
Assuming we had an instance of a World stored in a variable named world, what expression would correctly access the World's Save button's starting X coordinate?
Start from world and follow one field at a time: world.save is the Save Button, .box is its Rectangle, .start is that rectangle's starting Position, and .x is its X coordinate. Together: world.save.box.start.x.