Medium10 minPython Fundamentals
UpdatedAug 4, 2026
Edit

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

  • @dataclass can generate __init__, __repr__, and equality methods.
  • field(default_factory=...) creates a fresh mutable value per instance.
  • frozen=True makes attribute reassignment disallowed and can support value-object design.
  • Dataclasses reduce boilerplate but do not automatically validate external input.

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?”

Answers by Technology

+ Add Variant
PythonImprove this answer ✏️

Expected Answer (Python 3.14)

A dataclass is a class decorator for data-oriented classes. From declared fields it can generate an initializer, readable representation, and value equality; options control ordering, immutability-like behavior, slots, and more. It keeps the type explicit while removing repetitive boilerplate.

Mutable fields must use field(default_factory=...), which is called separately for each instance. Writing tags: list[str] = [] would share a list and is rejected by dataclasses for common mutable built-in defaults.

from dataclasses import dataclass, field

@dataclass(frozen=True, slots=True)
class Issue:
    number: int
    title: str
    labels: list[str] = field(default_factory=list)

issue = Issue(42, "Add type hints")
issue.labels.append("good-first-issue")
assert issue.labels == ["good-first-issue"]

frozen=True prevents attribute reassignment, but it does not recursively freeze a mutable field such as the list above.

Why It Matters

Dataclasses make domain models and internal transfer objects concise and readable. Understanding their generated behavior prevents shared defaults and incorrect assumptions about immutability or input validation.

Common Mistakes

  • Using a mutable literal as a field default: Use default_factory to create one object per instance.
  • Assuming frozen=True deeply freezes nested values: It blocks assigning attributes, not mutation inside a contained list or dictionary.
  • Treating a dataclass as automatic validation: Type annotations and generated initialization do not validate untrusted input by themselves.

Follow-up Questions

  • Why use slots=True? (Answer: It can reduce per-instance memory and prevents arbitrary instance attributes unless explicitly supported.)
  • How can a dataclass run post-initialization logic? (Answer: Implement __post_init__.)