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=[]
)—usedefault_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
, andunsafe_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()
andastuple()
for serialization; avoid for very large graphs due to recursion/copy cost.
Checks for Understanding
- How do you set a list field with an empty list default correctly?
- What does
frozen=True
actually prevent?
Show answers
field(default_factory=list)
- Rebinding attributes on the instance; it does not make nested objects immutable.
Exercises
- Create an immutable dataclass with ordering for
Point(x, y)
. Show sorting behavior. - Profile memory of a dataclass with and without
slots=True
for many instances.