Dataclasses (@dataclass, Python 3.7+) যেসব ক্লাসে প্রধানত ডেটা থাকে তাদের জন্য স্বয়ংক্রিয়ভাবে boilerplate methods তৈরি করে। __slots__ একটি অপ্টিমাইজেশন যা প্রতিটি instance-এর __dict__ এড়িয়ে মেমোরি কমায় এবং attribute access দ্রুত করে।
Dataclasses (@dataclass, Python 3.7+) যেসব ক্লাসে প্রধানত ডেটা থাকে তাদের জন্য স্বয়ংক্রিয়ভাবে boilerplate methods তৈরি করে। __slots__ একটি অপ্টিমাইজেশন যা প্রতিটি instance-এর __dict__ এড়িয়ে মেমোরি কমায় এবং attribute access দ্রুত করে।
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 annotated fields থেকে __init__, __repr__, __eq__ (এবং ঐচ্ছিকভাবে ordering/hashing) তৈরি করে — বিরক্তিকর, ত্রুটিপূর্ণ 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 instances কে immutable করে তোলে; field(default_factory=list) নিরাপদে mutable defaults প্রদান করে (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__
সাধারণত প্রতিটি instance একটি dynamic __dict__-এ attributes সংরক্ষণ করে। __slots__ পরিবর্তে attributes এর একটি নির্দিষ্ট সেট একটি compact, fixed structure-এ সংরক্ষণ করে — উল্লেখযোগ্য মেমোরি সাশ্রয় করে এবং access দ্রুত করে, তবে নমনীয়তার খরচে (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 একটি ব্যাপকভাবে ব্যবহৃত আধুনিক বৈশিষ্ট্য যা ডেটা-ধারণকারী ক্লাসের জন্য পুনরাবৃত্তিমূলক boilerplate দূর করে — কোড পরিষ্কার, কম ত্রুটিপূর্ণ এবং আরও পাঠযোগ্য করে তোলে (স্বয়ংক্রিয় __init__/__repr__/__eq__, immutability এবং নিরাপদ defaults সহ)।
এটি যখন আপনার mutability/methods প্রয়োজন তখন manually এমন ক্লাস লেখা বা namedtuple ব্যবহার করার আধুনিক idiomatic বিকল্প। __slots__ একটি লক্ষ্যবদ্ধ performance অপ্টিমাইজেশন যা বিশাল সংখ্যক objects instantiate করার সময় মেমোরি উল্লেখযোগ্যভাবে কমায় এবং attribute access দ্রুত করে।
@dataclass দৈনন্দিন পরিষ্কার ডেটা মডেলিং-এর জন্য এবং __slots__ (এখন slots=True এর মাধ্যমে সমন্বিতযোগ্য) মেমোরি-সংকটপূর্ণ hot paths-এর জন্য — উভয়ই জানা efficient, maintainable Python ক্লাস লেখার বর্তমান best practices প্রতিফলিত করে।