Dataclasses (@dataclass, Python 3.7+) auto-generate boilerplate methods for classes that mainly hold data. __slots__ is an optimization that reduces memory and speeds up attribute access by avoiding the per-instance __dict__.
Dataclasses (@dataclass, Python 3.7+) auto-generate boilerplate methods for classes that mainly hold data. __slots__ is an optimization that reduces memory and speeds up attribute access by avoiding the per-instance __dict__.
from dataclasses import dataclass
# ❌ without dataclass — lots of repetitive boilerplate
class Point:
def __init__(self, x, y):
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, self.y) == (other.x, other.y)
# ✅ with @dataclass — all of the above generated automatically
@dataclass
class Point:
x: int
y: int
p = Point(1, 2)
p # Point(x=1, y=2) — __repr__ generated
p == Point(1, 2) # True — __eq__ generated
@dataclass generates __init__, __repr__, __eq__ (and optionally ordering/hashing) from the annotated fields — removing tedious, error-prone boilerplate.
from dataclasses import dataclass, field
@dataclass(frozen=True) # frozen → immutable (hashable, usable as dict key)
class Config:
name: str
tags: list = field(default_factory=list) # mutable default done safely
timeout: int = 30 # default value
frozen=True makes instances immutable; field(default_factory=list) safely provides mutable defaults (avoiding the shared-mutable-default trap).
# normally, each instance has a __dict__ to store attributes (flexible but memory-heavy)
class Regular:
def __init__(self, x, y): self.x, self.y = x, y
# __slots__ → fixed attribute set, NO per-instance __dict__
class Slotted:
__slots__ = ("x", "y") # only these attributes allowed
def __init__(self, x, y): self.x, self.y = x, y
s = Slotted(1, 2)
s.z = 3 # ❌ AttributeError — can't add attributes not in __slots__
Normally each instance stores attributes in a dynamic __dict__. __slots__ declares a fixed set of attributes stored in a compact, fixed structure instead — saving significant memory and speeding up access, at the cost of flexibility (no adding arbitrary attributes).
@dataclass(slots=True) # Python 3.10+ — dataclass WITH __slots__
class Point:
x: int
y: int
@dataclass → any class that's mostly data (DTOs, configs, records) — cleaner, less code
__slots__ → when you create MANY instances (millions) and memory/speed matters
(e.g. nodes in a big data structure, large simulations)
Dataclasses are a widely-used modern feature that eliminates repetitive boilerplate for data-holding classes — making code cleaner, less error-prone, and more readable (auto-generated __init__/__repr__/__eq__, plus immutability and safe defaults).
They're the idiomatic modern alternative to manually writing such classes or using namedtuple when you need mutability/methods. __slots__ is a targeted performance optimization that meaningfully reduces memory and speeds attribute access when you instantiate huge numbers of objects.
Knowing both — @dataclass for everyday clean data modeling and __slots__ (now combinable via slots=True) for memory-critical hot paths — reflects current best practices for writing efficient, maintainable Python classes.