Python Dataclasses
CONCEPTS:Python Dataclasses
Question Variations
- "Which methods does `@dataclass` generate by default?"
- "How do you give a dataclass list field a safe default?"
- "What does `frozen=True` guarantee and not guarantee?"
- "When would you choose a regular class instead?"
Why This Is Asked
Dataclasses are a standard way to model data in modern Python. This question tests whether you know what they generate, when their defaults are safe, and when a regular class or a validation-focused model is a better fit.
Key Concepts
@dataclasscan generate__init__,__repr__, and equality methods.field(default_factory=...)creates a fresh mutable value per instance.frozen=Truemakes attribute reassignment disallowed and can support value-object design.- Dataclasses reduce boilerplate but do not automatically validate external input.
Question Variations
- “Which methods does
@dataclassgenerate by default?” - “How do you give a dataclass list field a safe default?”
- “What does
frozen=Trueguarantee and not guarantee?” - “When would you choose a regular class instead?”