Dataclasses
Checking access...
Dataclasses automatically generate __init__, __repr__, __eq__, and more.
Basic Dataclass
from dataclasses import dataclass
@dataclassclass Point: x: float y: float
# __init__ auto-generatedp1 = Point(3.0, 4.0)p2 = Point(3.0, 4.0)
# __repr__ auto-generatedprint(p1) # Point(x=3.0, y=4.0)
# __eq__ auto-generatedprint(p1 == p2) # TrueDefault Values and Field Types
from dataclasses import dataclass, fieldfrom typing import List
@dataclassclass Student: name: str grades: List[float] = field(default_factory=list) active: bool = True year: int = 1
s = Student("Alice")print(s) # Student(name='Alice', grades=[], active=True, year=1)Caution
Never use mutable defaults like grades=[] directly — always use field(default_factory=list).
Immutable Dataclasses
from dataclasses import dataclass
@dataclass(frozen=True)class Config: host: str port: int = 5432
cfg = Config("localhost")# cfg.port = 8080 # FrozenInstanceError!Advanced Field Configuration
from dataclasses import dataclass, field
@dataclassclass Product: name: str price: float quantity: int = 0
# Computed field — not in __init__ @property def total_value(self) -> float: return self.price * self.quantity
# Post-init processing def __post_init__(self): if self.price < 0: raise ValueError("Price cannot be negative")Inheritance with Dataclasses
from dataclasses import dataclass
@dataclassclass Person: name: str age: int
@dataclassclass Employee(Person): employee_id: str department: str = "Engineering"
emp = Employee("Alice", 30, "E001")print(emp) # Employee(name='Alice', age=30, employee_id='E001', department='Engineering')Comparison and Ordering
from dataclasses import dataclass
@dataclass(order=True)class Priority: level: int description: str = field(compare=False) # exclude from comparison
tasks = [Priority(3, "low"), Priority(1, "high"), Priority(2, "medium")]sorted_tasks = sorted(tasks) # Sorted by levelprint([t.level for t in sorted_tasks]) # [1, 2, 3]Serialization
from dataclasses import dataclass, asdict, astupleimport json
@dataclassclass User: name: str email: str age: int
user = User("Alice", "alice@example.com", 30)
# To dictprint(asdict(user))# {'name': 'Alice', 'email': 'alice@example.com', 'age': 30}
# To tupleprint(astuple(user))# ('Alice', 'alice@example.com', 30)
# To JSONprint(json.dumps(asdict(user)))# {"name": "Alice", "email": "alice@example.com", "age": 30}Dataclass vs Regular Class Comparison
| Feature | Regular Class | Dataclass |
|---|---|---|
__init__ | Manual | Auto |
__repr__ | Manual | Auto |
__eq__ | Manual | Auto |
__hash__ | Manual | Auto (if frozen) |
__lt__, etc. | Manual | With order=True |
| Boilerplate | Lots | Minimal |
Key Takeaways
@dataclassauto-generates__init__,__repr__,__eq__- Use
field(default_factory=list)for mutable defaults frozen=Truemakes instances immutable and hashable__post_init__runs after__init__for validationasdict()andastuple()convert to standard types- Dataclasses work well with JSON serialization
- Available since Python 3.7 (or
dataclassesbackport for 3.6)