7.3 Using frozen dataclasses to collect data
The third technique for collecting data into a complex structure is the frozen @dataclass
. The idea is to create a class that is an immutable collection of named attributes.
Following the example from the previous section, we can have nested dataclasses such as the following:
from dataclasses import dataclass
@dataclass(frozen=True)
class PointDC:
latitude: float
longitude: float
@dataclass(frozen=True)
class LegDC:
start: PointDC
end: PointDC
distance: float
We’ve used a decorator, @dataclass(frozen=True)
, in front of the class definition to create an immutable (known as ”frozen”) dataclass. The decorator will add a number of functions for us, building a fairly sophisticated class definition without our having to provide anything other...