Records: class vs dataclass vs namedtuple
P28.data-modelling.01 · Audience: guest, it-ml, language-pro
Welcome to Objects & Data Modelling — the first pillar of the Advanced
Python discipline. Python gives you several ways to model a bundle of data, and
picking the right one is a daily design decision. This ladder starts there:
a plain class, a dataclass, and a namedtuple, and when each earns its keep.
Each rung below is a three-panel workspace: instructions on the left, a code editor in the middle, and output + test results on the right.
- Run checks the visible tests you can read in the instructions.
- Submit grades your code against hidden tests (extra edge cases), so you can't pass by hard-coding the shown answers.
- The reference solution unlocks once you pass.
The code runs entirely in your browser (Pyodide — Python compiled to WebAssembly); nothing leaves your machine.
Ask the mentor about this module
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Rung 1 — reach for a dataclass
Loading exercise…
Rung 2 — rebuild the record from scratch
Loading exercise…
Interview rung — class vs dataclass vs namedtuple (free-form)
Loading exercise…
By the end you should be able to justify, out loud, why a given record is a
frozen dataclass and not a namedtuple — the kind of small, sharp decision
interviewers and reviewers probe.
Where next?
Later in Data Modelling
This module unlocks
Go up a level