Defaults, post-init, asdict, frozen and order
Change Point to frozen=False and see which lines now fail. Then add a field with a default before one without, and read the error.
A dataclass is a class for holding data. The @dataclass decorator reads the annotated fields you list and writes __init__, __repr__ and __eq__ for you, with options for defaults, immutability (frozen), ordering, slots and keyword-only fields.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write a dataclass Student with name: str, marks: list (default empty list) and passed: bool = False. Create two students, add a mark to one and show the other's list is still empty.
Write a frozen, ordered dataclass Version with major, minor, patch ints. Sort [Version(1,2,0), Version(1,10,0), Version(1,2,5)] and print the result.
Write a dataclass Order with price: float and quantity: int and a __post_init__ that raises ValueError if either is not positive, and a computed total field (field(init=False)).
Explain it without notes
What does @dataclass generate, and what does it save you from writing?
Why can't you write items: list = [] in a dataclass, and what is the fix?
What do frozen=True and order=True do?
When would you choose a NamedTuple over a dataclass, and the reverse?
Expected output
Book(title='Python', pages=300, tags=[], in_stock=True)
True False
['lang', 'fast'] []
['new'] []
{'title': 'Go', 'pages': 250, 'tags': ['lang', 'fast'], 'in_stock': True}
['title', 'pages', 'tags', 'in_stock']
Book(title='Go', pages=999, tags=['lang', 'fast'], in_stock=True)
ValueError: pages must be positive
TypeError: unhashable type: 'Book'
Point(x=1, y=2) True [Point(x=1, y=5), Point(x=2, y=0)]
1
AttributeError: cannot assign to field 'x'