What is Python's dataclasses module?
Why Interviewers Ask This
This tests whether you can apply Python knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.
Answer
Dataclasses (Python 3.7+, PEP 557) reduce boilerplate for classes that mainly store data. The @dataclass decorator automatically generates __init__, __repr__, and __eq__ based on annotated fields. @dataclass class Point: x: float; y: float; label: str = "origin". Make immutable with frozen=True — generates __hash__ and raises FrozenInstanceError on modification. field(default_factory=list) for mutable defaults. __post_init__ for post-initialization logic. dataclasses.asdict() converts to dict. dataclasses.fields() inspects fields. Compare to: namedtuple (immutable, tuple-based), attrs (third-party, more features), Pydantic (with runtime validation). Dataclasses are excellent for DTOs, configuration objects, and value objects with automatic boilerplate generation.
Pro Tip
Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex Python answers easy to follow.