Use a Dataclass as a Plain Record
Use annotated fields to define a plain record with generated initialization, display, and equality, then read the narrow meaning of frozen=True.
A summary contains several values that belong together: how many readings were accepted, their mean, and the unit used to interpret them. A plain dataclass gives those values field names without repeating routine class setup.
Declare the Fields
Import dataclass, apply its decorator, and annotate each field:
The three annotations describe the expected value of each field. As in the previous lesson, they do not perform runtime conversion or validation.
The decorator asks dataclass to generate ordinary record setup from those
field declarations. We can construct a record with named arguments:
summary = Summary(count=5, mean=21.3, unit="°C")
The generated initializer assigns each argument to the corresponding instance field. We can then select fields with dot notation:
The field order in the class also supplies the positional order, but named construction makes the role of each value visible at the call.
Q1. Construct the summary by field name
Complete the record construction for five accepted readings, mean 21.3, and
unit "°C".
Editable Python
Ready to run.
HintUse all three field names
Write Summary(count=5, mean=21.3, unit="°C").
SolutionConstruct one named record
summary = Summary(count=5, mean=21.3, unit="°C")
Use Generated Display and Equality
Printing the record uses a generated representation that names both the class and its fields:
print(summary)
Summary(count=5, mean=21.3, unit='°C')
That display is useful while inspecting a program because the values are not separated from their roles.
Dataclasses also generate field-by-field equality:
first and second are different instances, but their corresponding fields
are equal. Changing one field value changes the equality result. This generated
behavior is appropriate for a plain record whose fields define its value.
Q2. Predict record equality
What does this expression produce?
Summary(5, 21.3, "°C") == Summary(5, 21.3, "°F")
Select one choice, then check.
HintCheck every declared field
Equality does not stop after the two numerical fields.
SolutionThe unit changes the record value
The expression is False. Both records have the same count and mean, but
"°C" != "°F".
Understand the Limit of frozen=True
With frozen=True, ordinary assignment to a field is blocked:
summary.unit = "K"
The assignment raises FrozenInstanceError. This protects the connection
among the values after construction: code cannot quietly relabel the same mean
by assigning another unit.
“Frozen” does not mean that every object reachable from a record becomes
immutable. If a frozen dataclass field referred to a list, the field could not
be assigned a different list, but the existing list could still be mutated.
frozen=True blocks ordinary field assignment; it is not a recursive freeze of
nested objects.
It also does not validate the field values. Python will construct this record:
invalid = Summary(count=-1, mean=21.3, unit="")
The annotations and decorator do not know the program's rules for a valid count or unit. Validation belongs at a boundary that understands those rules.
| Generated behavior | Meaning |
|---|---|
| initialization | accept values for declared fields |
| representation | display class and field values |
| equality | compare corresponding fields |
| frozen assignment | block ordinary reassignment of a field |
Q3. State what frozen records guarantee
Which statement describes @dataclass(frozen=True) accurately?
Select one choice, then check.
HintKeep the guarantee narrow
Ask what happens to summary.unit = "K", then ask whether the decorator knows
the program's valid units.
SolutionFrozen means blocked field assignment
Ordinary assignment to a field is blocked. A nested mutable object may still change, and invalid domain values are still accepted unless code validates them separately.
A dataclass turns annotated fields into a plain record with generated
initialization, display, and equality. frozen=True blocks ordinary field
assignment, but it neither freezes nested objects deeply nor validates the
record. The final lesson uses two such records to keep run settings separate
from returned evidence.