Python - Dataclasses

Overview

Estimated time: 25–35 minutes

@dataclass reduces boilerplate for classes that primarily store data. Learn fields, defaults, immutability (frozen=True), ordering, and common patterns.

Learning Objectives

  • Define dataclasses with type hints and default values.
  • Use field() for defaults and factories; control comparison and hashing.
  • Understand immutability (frozen) and slots for performance (slots=True, 3.10+).

Prerequisites

  • Basic classes and type hints

Examples

from dataclasses import dataclass, field
from typing import List

@dataclass
class User:
    id: int
    name: str
    tags: List[str] = field(default_factory=list)

u = User(1, "Ada")
u.tags.append("admin")
print(u)

Expected Output (repr may vary):

User(id=1, name='Ada', tags=['admin'])

Common Pitfalls

  • Using a mutable default directly (e.g., tags=[])—use default_factory=list instead.
  • Assuming frozen=True prevents mutation of contained mutable fields—it prevents attribute rebinding, not deep immutability.
  • Ordering and hashing depend on configuration; be explicit about eq, order, and unsafe_hash needs.

Best Practices

  • Use dataclasses for simple domain objects and configuration; consider Pydantic for validation-heavy models.
  • Prefer slots=True for large numbers of instances to save memory.
  • Expose asdict() and astuple() for serialization; avoid for very large graphs due to recursion/copy cost.

Checks for Understanding

  1. How do you set a list field with an empty list default correctly?
  2. What does frozen=True actually prevent?
Show answers
  1. field(default_factory=list)
  2. Rebinding attributes on the instance; it does not make nested objects immutable.

Exercises

  1. Create an immutable dataclass with ordering for Point(x, y). Show sorting behavior.
  2. Profile memory of a dataclass with and without slots=True for many instances.