Dunder methods & the data model
P28.data-modelling.02 · Audience: guest, it-ml, language-pro · Prerequisites: Records: class vs dataclass vs namedtuple
Python's data model is the set of dunder (double-underscore) methods that
let your objects plug into the language's syntax and built-ins: ==, <,
hash(), repr(), in, with, and more. Implement them well and your types
feel native; implement them carelessly and you get subtle bugs.
This ladder builds two value objects — a colour and a version — and wires them into equality, hashing, and ordering.
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 — value equality and hashing
Loading exercise…
Rung 2 — ordering with total_ordering
Loading exercise…
Interview rung — responsible operator overloading (free-form)
Loading exercise…
The recurring rule to internalise: objects that compare equal must hash
equal, and __eq__ should return NotImplemented (not False) for types it
doesn't recognise, so Python can try the reflected comparison.
Where next?
Later in Data Modelling
Go up a level