dataclass (@dataclass, Python 3.7+) สร้าง method ที่เป็น boilerplate โดยอัตโนมัติสำหรับ class ที่ส่วนใหญ่ใช้เก็บข้อมูล __slots__ เป็นการ optimize ที่ลดการใช้หน่วยความจำและเร่งความเร็วการเข้าถึง attribute ด้วยการหลีกเลี่ยง __dict__ ต่อ instance
dataclass (@dataclass, Python 3.7+) สร้าง method ที่เป็น boilerplate โดยอัตโนมัติสำหรับ class ที่ส่วนใหญ่ใช้เก็บข้อมูล __slots__ เป็นการ optimize ที่ลดการใช้หน่วยความจำและเร่งความเร็วการเข้าถึง attribute ด้วยการหลีกเลี่ยง __dict__ ต่อ 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 สร้าง __init__, __repr__, __eq__ (และเลือกได้ว่าจะให้มี ordering/hashing) จาก field ที่กำกับไว้ — กำจัด 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 ทำให้ instance เป็น immutable; field(default_factory=list) จัดหาค่าเริ่มต้นที่แก้ไขได้อย่างปลอดภัย (หลีกเลี่ยงกับดักของ 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 จะเก็บ attribute ไว้ใน __dict__ แบบไดนามิก __slots__ ประกาศชุด attribute ที่ตายตัวซึ่งเก็บไว้ในโครงสร้างที่กระชับและตายตัวแทน — ประหยัดหน่วยความจำได้อย่างมีนัยสำคัญและเร่งการเข้าถึง โดยแลกมาด้วยความยืดหยุ่น (ไม่สามารถเพิ่ม attribute ตามอำเภอใจได้)
@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)
dataclass เป็นฟีเจอร์สมัยใหม่ที่ใช้กันอย่างแพร่หลายซึ่งกำจัด boilerplate ที่ซ้ำซากสำหรับ class ที่ใช้เก็บข้อมูล — ทำให้โค้ดสะอาดขึ้น เกิดข้อผิดพลาดน้อยลง และอ่านง่ายขึ้น (สร้าง __init__/__repr__/__eq__ อัตโนมัติ บวกกับ immutability และค่าเริ่มต้นที่ปลอดภัย)
พวกมันเป็นทางเลือกสมัยใหม่ที่เป็นสำนวนถูกต้องแทนการเขียน class ดังกล่าวด้วยมือ หรือแทนการใช้ namedtuple เมื่อคุณต้องการความเป็น mutable/มี method __slots__ เป็นการ optimize ประสิทธิภาพแบบเจาะจงที่ลดหน่วยความจำและเร่งการเข้าถึง attribute ได้อย่างมีความหมายเมื่อคุณสร้าง object จำนวนมหาศาล
การรู้ทั้งสองอย่าง — @dataclass สำหรับการสร้างแบบจำลองข้อมูลที่สะอาดในชีวิตประจำวัน และ __slots__ (ซึ่งตอนนี้รวมเข้าด้วยกันได้ผ่าน slots=True) สำหรับ hot path ที่หน่วยความจำเป็นเรื่องสำคัญ — สะท้อนถึงแนวปฏิบัติที่ดีที่สุดในปัจจุบันสำหรับการเขียน class ของ Python ที่มีประสิทธิภาพและบำรุงรักษาง่าย