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.
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.
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.
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.
@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.
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.
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.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.
| Resource | Kind | Cost | Provider |
|---|---|---|---|
| The Python Tutorial: classes | reading | free | Python Software Foundation |
| dataclasses: data classes | reference | free | Python Software Foundation |
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.
No assessment yet.
No credential yet.
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.
No outcome builds on this yet. Ask for the next concept.