avatar Bite 11. Enrich a class with dunder methods

Let's enrich an Account class by adding dunder (aka special) methods to support the following:

  1. length of the object: len(acc) returns the number of transactions
  2. account comparison: acc1 >,<,>=.<=,== acc2 returns a boolean comparing account balances
  3. indexing: acc[n] shows the nth transaction onaccount (0 based)
  4. iteration: list(acc) returns a sequence of account transactions
  5. operator overloading: acc + int and acc - int can be used to add/subtract money (if given incompatible types for these operations, raise a TypeError - note that previously this was a ValueError, but former is more correct for this scenario)
  6. string representation: str(acc) returns NAME account - balance: INT

The provided template already does some setup for you.

Check out the tests for more specifics. If you're still new to dunder (magic) methods, here is an article you might find useful. Good luck!

Login and get coding
go back Advanced level
Bitecoin 4X

371 out of 394 users completed this Bite.
Will you be Pythonista #372 to crack this Bite?
Resolution time: ~47 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 4.12 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help