A
dataclassis a decorator from the built-indataclassesmodule that automatically generates boilerplate methods likeinit,reprandeqfor classes that primarily store data.It lets you write clean, readable data containers without manually typing repetitive initialization and comparison code.
from dataclasses import dataclass
@dataclass
class Product:
name: str
price: float
in_stock: bool = True
item1 = Product("Laptop", 999.99)
item2 = Product("Laptop", 999.99)
print(item1) # Output: Product(name='Laptop', price=999.99, in_stock=True)
print(item1 == item2) # Output: True (compares values, not memory addresses)