Dataclasses and Value Objects · practice
Set Dataclass Defaults
A generated constructor can have defaults just like a
The first call uses one point; the second overrides it. The default lives on the annotated field.
Required fields come first
Dataclasses follow the same rule as function
@dataclass
class BrokenQuestion:
points: int = 1
prompt: str
Put every required field first, then fields with defaults:
@dataclass
class QuizConfig:
title: str
pass_mark: float = 0.6
shuffle: bool = False
time_limit: int = 0
Here 0 means no time limit. Every default matches the type written beside it.
Why must title appear before pass_mark?
Defaults describe the common case
A good default removes noise from ordinary construction without hiding an important choice. One point and a 60 percent pass mark are reasonable ordinary cases. A quiz title is not, so it remains required.
Use None as direct defaults. A
The exercise focuses only on scalar defaults and their ordering.
Task
Add the scalar defaults each dataclass needs.
QuizConfig.title stays required. pass_mark defaults to 0.6, shuffle to False, and time_limit to 0, meaning no limit.
Question.prompt and answer stay required. points defaults to 1.
Keep required fields before fields with defaults, and verify that every default can still be overridden.
Keep the dataclasses valid in __post_init__. A config needs a non-empty title, a pass_mark above 0 and at most 1, and a non-negative time_limit. A question needs a non-empty prompt and answer and positive points.