Questions
4 of 19
1How would you make a custom class usable in a for loop?
2How do __enter__ and __exit__ work together to implement the context manager protocol?
3How does Python implement inheritance, and what is the Method Resolution Order (MRO)?
4What are dataclasses, and how do they reduce boilerplate compared to manually writing __init__, __eq__, and __repr__?
5What is the @property decorator, and what problem does it solve compared to plain getter/setter methods?
6What is the difference between method overriding and method overloading, and why doesn't Python natively support overloading?
7What problem does the diamond inheritance pattern cause, and how does Python's MRO resolve it?
8What is the difference between a class and an instance in Python?
9How does Python implement encapsulation given that it has no true 'private' access modifiers?
10What is the purpose of self in instance methods, and why must it be explicitly declared in Python?
11What is the difference between __str__ and __repr__, and what convention should __repr__ follow?
12What is a metaclass, and what is type's relationship to every class in Python?
13What is the purpose of __eq__ and __hash__, and why must they be implemented consistently?
14What is a descriptor, and how do @property and ORMs like Django's models rely on the descriptor protocol?
15What is the difference between an instance method, a class method, and a static method?
16What is composition, and why is 'favor composition over inheritance' often recommended?
17What are dunder (magic) methods, and how do they enable operator overloading?
18When would you use a metaclass instead of a class decorator or __init_subclass__?
19How do abstract base classes (abc.ABC) enforce interface contracts in Python?
04 / 19

What are dataclasses, and how do they reduce boilerplate compared to manually writing __init__, __eq__, and __repr__?

Difficulty: 5/10
Dataclasses, Boilerplate Reduction, Value Objects

@dataclass generates init, repr, and eq from class annotations

A dataclass is a normal class that the @dataclass decorator augments by reading annotations and generating boilerplate methods. By default it generates init, repr, and eq. With frozen=True it also generates hash and makes instances immutable. With order=True it generates comparison methods. Fields can be customized with field(), which supports default_factory, repr=False, and comparison flags. The result is concise, readable value objects with correct semantics. The trade-offs are that generated init can be awkward with complex validation, and that dataclasses are mutable by default, so hashability requires an explicit choice.

  1. 1

    @dataclass(eq=True, frozen=True) is the standard pattern for immutable value objects that need to be hashable.

  2. 2

    @dataclass(order=True) generates ordering based on field order; be explicit about the field order.

  3. 3

    field(default_factory=list) is required for mutable defaults. Using a list directly raises ValueError in 3.11+.

  4. 4

    Trade-off: dataclasses generate methods you cannot easily customize per field. For complex validation or serialization, use Pydantic or attrs.

  5. 5

    Common mistake: assuming dataclasses are immutable. They are mutable unless frozen=True.

  6. 6

    Common mistake: defining a field with a mutable default and sharing it across instances.

  7. 7

    Version note: dataclasses landed in 3.7. slots=True arrived in 3.10, kw_only in 3.10, and 3.11 added stricter mutable-default checks and improved error messages.

Scenario Questions

0-2 years experience

  1. 1How do you create a simple value object without writing __init__ and __repr__?
  2. 2What is the default value of eq and frozen in @dataclass?

2-5 years experience

  1. 1You need a dataclass field that is a list per instance. What do you use instead of a mutable default?
  2. 2You need ordering by a specific field order. How do you enable and control it?

5-8 years experience

  1. 1You need a dataclass with validation and a canonical serialization format. How do you extend dataclasses without losing ergonomics?
  2. 2You need a dataclass with slots to reduce memory. What changes in behavior and what breaks with inheritance?

8+ years experience

  1. 1Design a domain model library using dataclasses that supports immutability, validation, serialization, and versioning without duplicating field metadata.
  2. 2Compare dataclasses, attrs, and Pydantic on performance, validation, typing, and migration cost, and recommend a strategy for a large codebase.

Follow-up Questions

  • How do you make a dataclass immutable and hashable?
  • When would Pydantic or attrs be a better choice than a dataclass?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.