ડેટાક્લાસ (@dataclass, Python 3.7+) ક્લાસ માટે બોઇલરપ્લેટ મેથડ્સ સ્વચાલિતપણે તૈયાર કરે છે જે મુખ્યત્વે ડેટા ધરાવે છે. __slots__ એક ઑપ્ટિમાઇઝેશન છે જે મેમરી ઘટાડે છે અને પ્રતિ-ઇન્સટેન્સ __dict__ ટાળીને એટ્રિબ્યુટ એક્સેસને ઝડપી બનાવે છે.
ડેટાક્લાસ (@dataclass, Python 3.7+) ક્લાસ માટે બોઇલરપ્લેટ મેથડ્સ સ્વચાલિતપણે તૈયાર કરે છે જે મુખ્યત્વે ડેટા ધરાવે છે. __slots__ એક ઑપ્ટિમાઇઝેશન છે જે મેમરી ઘટાડે છે અને પ્રતિ-ઇન્સટેન્સ __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 એનોટેટેડ ફીલ્ડમાંથી __init__, __repr__, __eq__ (અને વૈકલ્પિક રીતે ઓર્ડરિંગ/હેશિંગ) તૈયાર કરે છે — કંટાળાજનક, ભૂલ-સંભવ બોઇલરપ્લેટ દૂર કરે છે.
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 ઇન્સટેન્સ્સને અપરિવર્તનીય બનાવે છે; field(default_factory=list) સુરક્ષિત રીતે મ્યુટેબલ ડિફૉલ્ટ પ્રદાન કરે છે (શેર્ડ-મ્યુટેબલ-ડિફૉલ્ટ વીણને ટાળીને).
# 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__
સામાન્યતયા દરેક ઇન્સટેન્સ ડાયનેમિક __dict__ માં એટ્રિબ્યુટ સંગ્રહ કરે છે. __slots__ એટ્રિબ્યુટ્સનો નિશ્ચિત સમૂહ ઘોષણા કરે છે જે કોમ્પેક્ટ, નિશ્ચિત સ્ટ્રક્ચરમાં સંગ્રહવામાં આવે છે તેવલી જગ્યાએ — નોંધપાત્ર મેમરી બચાવે છે અને એક્સેસને ઝડપી બનાવે છે, લવચીકતાની કિંમતે (મનસુવાર એટ્રિબ્યુટ્સ ઉમેરવામાં આવતી નથી).
@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)
ડેટાક્લાસ એક વ્યાપક-ઉપયોગમાં લેવાતો આધુનિક ફીચર છે જે ડેટા-ધરાવતી ક્લાસ માટે પુનરાવર્તીય બોઇલરપ્લેટ દૂર કરે છે — કોડ સાફ, ઓછી ભૂલ-સંભવ અને વધુ વાંચનીય બનાવે છે (સ્વચાલિત __init__/__repr__/__eq__, વધુમાં અપરિવર્તનીયતા અને સુરક્ષિત ડિફૉલ્ટ્સ).
તેઓ અગ્રણી આધુનિક વિકલ્પ છે મેમુઅલી આવી ક્લાસ લખવાની અથવા namedtuple ઉપયોગ કરવાની જ્યારે તમને મ્યુટેબિલિટી/મેથડ્સ જોઈએ. __slots__ એક ધતણ કરેલ પર્ફરમેન્સ ઑપ્ટિમાઇઝેશન છે જે અર્થપૂર્ણ રીતે મેમરી ઘટાડે છે અને વિશાળ સંખ્યાએ ઑબ્જેક્ટ્સ ઇન્સટેન્ટ કરવા સમયે એટ્રિબ્યુટ એક્સેસને ઝડપી બનાવે છે.
બંને જાણવું — દૈનિક સ્વચ્છ ડેટા મોડેલિંગ માટે @dataclass અને મેમરી-ક્રિટિકલ હોટ પાથ માટે __slots__ (હવે slots=True વિશે સંયોજનીય) — કાર્યક્ષમ, જાળવણીપાત્ર Python ક્લાસ લખવાના વર્તમાન શ્રેષ્ઠ પ્રથાઓ પ્રતિબિંબિત કરે છે.