Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
Dunder methods let objects integrate with Python syntax and built-ins; a Money class would implement __init__, __repr__, __eq__, __hash__, __lt__ and arithmetic methods like __add__.
Python's data model maps operations to special methods: a + b calls add, len(x) calls len, str(x) calls str, and x == y calls eq. For Money, repr gives a clear debugging representation, eq and hash make equal amounts in the same currency interchangeable and usable in sets, and ordering methods, or functools.total_ordering, enable sorting. add should check the currency and return NotImplemented for unsupported types so Python can try the other operand. Amounts should be stored as Decimal or integer minor units rather than float, and a frozen dataclass can generate much of this automatically.