ਡੇਟਾਕਲਾਸਜ਼ (@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 ਦੁਆਰਾ ਜੋੜ ਯੋਗ) ਮੈਮੋਰੀ-ਮਹੱਤਵਪੂਰਣ ਗਰਮ ਪਾਬ ਲਈ — ਮੁਢਲੇ ਕਲਾਸਜ਼ ਲਿਖਣ ਲਈ ਮੌਜੂਦਾ ਸਰਵ-ਸ਼੍ਰੇਸ਼ਠ ਅਭਿਆਸ ਨੂੰ ਪ੍ਰਤੀਬਿੰਬਿਤ ਕਰਦਾ ਹੈ।