Skip to content

Model data with classes and dataclasses

I can define a class that holds related data and the behaviour belonging to it, use a dataclass when the class is mostly data, and say when a dict or a function would have been the better choice.

Domain
Python Programming
Level
intermediate
Status
draft · v0.1.0
Also called
Python classes, Object-oriented Python, dataclass, __init__
License
CC BY-SA 4.0

Learn first

Learn it

Scope

Giving a name to a thing your program keeps talking about, and putting the behaviour that belongs to it in the same place.

In scope: defining a class, __init__, instance attributes and methods, dataclass, and judging when not to. Out of scope: inheritance hierarchies, properties, metaclasses and protocols.

When a class earns its place

A class is warranted when the same group of values keeps travelling together and the same operations keep being done to them. The signal is in the code you already have: three parallel lists indexed in step, a dict whose keys you must remember, a function taking six arguments that are really one thing.

It is not warranted when there is no behaviour. A bag of values that nothing acts on is a dict, or a dataclass if you want dotted access and a readable repr. A single operation with no state is a function. Most Python is functions on plain data, and that is a good thing.

What a dataclass gives you

@dataclass writes the boilerplate: __init__, a __repr__ that prints the field values, and __eq__ comparing field by field rather than by identity. That last one is the reason it matters in testing: two records with the same contents compare equal, so assert result == Expected(...) says what you mean.

Declare fields with types; frozen=True makes instances immutable, which makes them hashable and safe to share.

The method question

A method belongs on the class when it needs the instance’s own data. order.total() reads better than total(order) and keeps the calculation next to the fields it uses. A function that merely takes an instance and returns something unrelated is a function; putting it on the class only makes the class longer.

Common misconceptions

  • “Good Python is object-oriented.” Good Python is whatever makes the code plain. Classes are one tool; a great deal of excellent Python has none.
  • “A dataclass is a struct.” It is a normal class with generated methods. You can add your own and it is still a dataclass.
  • “self is passed magically.” self is the instance, passed as the first argument by the attribute lookup. Knowing that makes the TypeError about arguments obvious.
  • “Class attributes are per-instance defaults.” A mutable class attribute is shared by every instance, the same trap as a mutable default argument.

Notes for teachers

Start from code that hurts: three lists kept in step, or a dict whose shape is documented in a comment. Ask learners to name the thing, then introduce the class as the name becoming code.

Then ask for the opposite case — somewhere a class was written and a function would do — so the judgement runs both ways.

Resources

ResourceKindCostProvider
The Python Tutorial: classesreadingfreePython Software Foundation
dataclasses: data classesreferencefreePython Software Foundation

Courses that include it

draft

Python for a Coding Assessment

A refresher path through the Python graph for someone who has written Python before and has an assessment coming: run and read, rebuild the core, then work the way an assessment expects.

Prove it

Evidence of mastery

  • Replaces a group of parallel lists or a dict of dicts with a class or dataclass, and the code reads more clearly afterwards.
  • Writes `__init__` and one method that operates on the instance's own data.
  • Uses a dataclass and explains which methods it generated and what they do.
  • Argues, for a given case, that a class is not warranted and a function or a dict is enough.

Assessments that measure it

No assessment yet.

Credentials that include it

No credential yet.

What learners show

Nothing to show yet. The publisher last published on 2026-09-20, and no aggregate for this outcome has reached the 50 learners a number needs. Open Degree does not track learners. About learner evidence.

What comes next

No outcome builds on this yet. Ask for the next concept.