We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
Modelling Data with Dataclasses
PythonWrite classes that hold data with dataclasses, and control their defaults, equality, hashing and memory use. Choose between a dataclass, NamedTuple and TypedDict, and turn outside data into typed records your code can trust.
This track uses Python. You can make it your practice mode to browse related content across the site.
Your learning sequence
- 1 What a Dataclass Writes for You Read
- 2 Not solved yet. Sort Version Numbers with a Dataclass Practice · Easy
- 3 Not solved yet. Find Mutable Defaults That Instances Share Practice · Easy
- 4 Not solved yet. Hide Secret Fields When You Print a Record Practice · Medium
- 5 Not solved yet. Compute Cents from an Amount with __post_init__ Practice · Medium
- 6 Not solved yet. Fix a Dataclass Hierarchy with Keyword-Only Fields Practice · Medium
- 7 Not solved yet. Tell Fields Apart from Class Variables Practice · Easy
- 8 Why Some Objects Can Go in a Set Read
- 9 Not solved yet. Count Visited Cells with a Frozen Dataclass Practice · Easy
- 10 Not solved yet. Freeze Nested Data into a Hashable Cache Key Practice · Medium
- 11 Not solved yet. Predict Which Dataclasses Are Hashable Practice · Hard
- 12 Not solved yet. Sort Semantic Versions with Prerelease Labels Practice · Hard
- 13 Save Memory with slots=True Read
- 14 Not solved yet. Check a Class Before You Add slots=True Practice · Hard
- 15 Choose Between a Dataclass, NamedTuple and TypedDict Read
- 16 Not solved yet. Merge Overlapping Ranges with a NamedTuple Practice · Medium
- 17 Not solved yet. Update a Frozen Record with replace() Practice · Hard
- 18 Not solved yet. Convert Dataclasses to JSON-Ready Values Practice · Hard
- 19 Not solved yet. Parse Untrusted JSON into a Dataclass Practice · Hard
- 20 Not solved yet. Build a JSON Codec for Dataclasses Practice · Hard
- 21 Validate a Field on Every Assignment with a Descriptor Read
- 22 Not solved yet. Convert and Validate a Port Number on Assignment Practice · Medium
- 23 Not solved yet. Build a Generic Result Type with Ok and Err Practice · Hard
- 24 Not solved yet. Diff Two Dataclass Records Field by Field Practice · Hard
- 25 Not solved yet. Type-Check a Custom Dataclass Decorator with dataclass_transform Practice · Hard
- 26 Build Dataclasses at Runtime with make_dataclass Read
- 27 Not solved yet. Resolve Dataclass Field Types in Python 3.14 Practice · Hard
- 28 Dataclass Changes Across Python Versions Read
Check yourself
5 questions · not saved while you're signed outUse these questions to check your understanding and compare explanations. Your score does not affect track completion.
Why does Python reject Bad, while each Good object gets its own list?
Choose one answer to reveal the explanations. Answers are kept only while this page is open.
from dataclasses import dataclass, field
try:
@dataclass
class Bad:
xs: list[int] = []
except ValueError:
print("Bad rejected")
@dataclass
class Good:
xs: list[int] = field(default_factory=list)
a, b = Good(), Good()
a.xs.append(1)
print(a.xs, b.xs)
print(a.xs is b.xs)