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

Command/Ctrl + Enter. Python runs in your browser.

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")
Not attempted
Review

Not marked done.

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")
Choose one

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".

Not attempted
Review

Not marked done.

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 behaviorMeaning
initializationaccept values for declared fields
representationdisplay class and field values
equalitycompare corresponding fields
frozen assignmentblock ordinary reassignment of a field

Q3. State what frozen records guarantee

Which statement describes @dataclass(frozen=True) accurately?

Choose one

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.

Not attempted
Review

Not marked done.

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.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.