You’ll be able to
- Customize object representation with
__repr__and__str__ - Enable arithmetic with
__add__,__mul__,__sub__ - Support comparison with
__eq__,__lt__,functools.total_ordering - Make objects iterable with
__iter__and__next__
Loading lesson…
Why this matters
Pandas' DataFrame.__getitem__, SQLAlchemy's operator overloading, and pathlib's / operator are all dunder tricks. Staff-level interviews often probe __eq__/__hash__ contracts and the __init__ vs __new__ split — knowing when to override __new__ matters for singletons, immutables, and metaclass work.
Common pitfalls
- Overriding
__eq__without__hash__— silently makes the class unhashable and breaksset/dictkeys. - Writing
__repr__that isn't a valideval()target — good__repr__should reconstruct the object for debug logs. - Confusing
__str__(human) with__repr__(developer) —print()falls back to__repr__only when__str__is absent.