Money with __repr__ and __eq__
Medium · Classes
By default, printing an object shows something like <Money object at 0x...>, and == only checks whether two variables point to the same object. Define __repr__ to return a helpful string, and __eq__ to compare by value. When __eq__ gets an object of another type, return NotImplemented (not False), so Python can try the other side and then fall back to False.
Add to Money:
- __repr__ returning Money(<cents>, <currency>) in Python syntax, for example Money(1299, 'USD'). Tip: f"{value!r}" uses repr().
- __eq__ that is True when both cents and currency match.
The driver compare_money is already written: it returns both reprs, m1 == m2, and m1 == a (a plain list, which must be False).