Dataclasses (@dataclass, Python 3.7+) otomatis ngasilake metode boilerplate kanggo kelas sing utamane nyekel data. __slots__ minangka optimisasi sing nyuda memori lan nyepat akses atribut kanthi ngindhari __dict__ per-instance.
Dataclasses (@dataclass, Python 3.7+) otomatis ngasilake metode boilerplate kanggo kelas sing utamane nyekel data. __slots__ minangka optimisasi sing nyuda memori lan nyepat akses atribut kanthi ngindhari __dict__ per-instance.
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 ngasilake __init__, __repr__, __eq__ (lan opsional ordering/hashing) saka field sing dijupuk anotasi — ngilangan boilerplate sing molu lan mudah kliru.
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 ndadekake instance immutable; field(default_factory=list) kanthi aman nyedhiyake default mutable (ngindhari jebakan shared-mutable-default).
# 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__
Biasane saben instance nyimpen atribut ing __dict__ dinamis. __slots__ ngumumake sakumpulan atribut tetep sing disimpen ing struktur tetep kompak — nyuda memori signifikan lan nyepat akses, kanthi biaya fleksibilitas (ora bisa nambah atribut sembarange).
@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 minangka fitur modern sing akeh dienggo sing ngilangi boilerplate berulang kanggo kelas sing nyekel data — ndadekake kode luwih resik, luwih ora mudah kliru, lan luwih bisa diwaca (auto-generated __init__/__repr__/__eq__, plus immutability lan default aman).
Dheweke alternatif idiomatic modern kanggo nulis kelas kanthi manual utawa ngenggo namedtuple manawa sampeyan butuh mutability/metode. __slots__ minangka optimisasi performa sing dukuh sing nyuda memori bermakna lan nyepat akses atribut manawa sampeyan nyipta jumlah besar obyek.
Ngerti loro-lorone — @dataclass kanggo modeling data resik saben dina lan __slots__ (saiki bisa digabungake liwat slots=True) kanggo jalur panas kritis memori — nunjukake praktik terbaru kanggo nulis kelas Python sing efisien lan mudah dijaga.