Data Classes in Python
dataclasses reduce boilerplate for classes that primarily store data.
Basics
Auto‑generate __init__, __repr__, __eq__, and more.
from dataclasses import dataclass
@dataclass
class User:
id: int
name: str
Defaults and factory
Use default values or factories for mutable fields.
from dataclasses import field
from typing import List
@dataclass
class Project:
name: str
members: List[str] = field(default_factory=list) # new list per instance
Ordering and hashing
Enable ordering methods and hashing when appropriate.
@dataclass(order=True, frozen=False)
class Point:
x: int
y: int
Immutability
frozen=True makes instances immutable (raises on assignment). Combine with tuples/frozenset for deep immutability.
@dataclass(frozen=True)
class Config:
host: str
port: int
Post‑init processing
__post_init__ runs after auto‑init for validation/normalization.
@dataclass
class Temperature:
celsius: float
def __post_init__(self):
if self.celsius < -273.15:
raise ValueError("below absolute zero")
Slots and performance (3.10+)
Use slots=True to reduce memory and speed attribute access.
@dataclass(slots=True)
class Light:
on: bool
Asdict/astuple and replace
Serialize or derive modified copies.
from dataclasses import asdict, astuple, replace
asdict(User(1, "Ada"))
replace(Config("localhost", 80), port=443)
Summary
- Use
@dataclassfor concise, typed records - Prefer
default_factoryfor mutable fields;frozenandslotsfor safety/perf