Dataclass (Python)

A Python dataclass is a decorator (@dataclass) introduced in Python 3.7 that automatically generates special methods for classes that are primarily used to store data—such as __init__(), __repr__(), __eq__(), etc.

Example

from dataclasses import dataclass
 
@dataclass
class Point:
    x: float
    y: float
 

The code above is roughly equivalent to manually writing:

class Point:
    def __init__(self, x: float, y: float):
        self.x = x
        self.y = y
 
    def __repr__(self):
        return f"Point(x={self.x}, y={self.y})"
 
    def __eq__(self, other):
        return self.x == other.x and self.y == other.y
from dataclasses import dataclass, field

@dataclass(frozen=True, slots=True)
class Config:
    lr: float = 3e-4
    batch_size: int = 64
    tags: tuple[str, ...] = ()

cfg = Config(lr=1e-3)
  • frozen=True for immutable-ish values
  • slots=True for memory savings
  • field(default_factory=…) for mutable defaults