Value Equality for Money

Hard ⏱ 18 min 35% acceptance ★★★★★ 4.5
By default two Money(10, 'INR') instances are unequal (identity comparison). Implement __eq__ comparing amount and currency — returning NotImplemented for foreign types — and a consistent __hash__ so equal Money objects can live in sets and as dict keys.

Examples

Example 1
Input
Money(10, 'INR') == Money(10, 'INR')
Output
True
Explanation

Value semantics replace identity.

Example 2
Input
len({Money(10, 'INR'), Money(10, 'INR')})
Output
1
Explanation

Equal objects hash identically and collapse in a set.

Constraints

  • __eq__ returns NotImplemented for non-Money.
  • __hash__ must agree with __eq__.

Topics

OOP__eq__ and __hash__

Companies

Goldman SachsBloombergMorgan Stanley

Hints

Hint 1

Compare a tuple of both fields.

Hint 2

hash((self.amount, self.currency)) keeps the contract.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.