When a class mostly stores data, its __init__ method can repeat the same assignment boilerplate. The @dataclass decorator generates that initializer, comparisons, and an improved __repr__, letting the class focus on its useful behavior. section 9.10
A minimal data class
@dataclass class Scoop: flavor: str The flavor annotation declares the field; the decorator creates the initializer and other class methods. Data-class attributes require type annotations, although Python itself does not enforce their types. section 9.10
A mutable collection field
from dataclasses import dataclass, field @dataclass class Bowl: scoops: list = field(default_factory=list) def add_scoops(self, *new_scoops): for scoop in new_scoops: self.scoops.append(scoop) This combines generated construction with an explicit method for changing the collection.






