Python Magic Methods (Dunder) Quiz

This Python dunder methods quiz looks at the special "magic" methods that let your objects work with built-in syntax. You will trace __str__ vs __repr__, equality and hashing, operator methods and iteration, with the gotchas that come up in interviews.

Hard 10 questions ~8 minutes Pass at 80% Python 3.12

Choose a mode

Playing as a guest: sign in to keep your XP and streak.

Sample question

Question 1 of 10

What does this print?

class P:
    def __repr__(self):
        return "P()"
    def __str__(self):
        return "a point"

p = P()
print(p, [p])
Show the answer

B. a point [P()] print() uses __str__ for the object itself. A list shows its items with repr(), so inside the list you see P().

What this quiz covers

  • __str__ vs __repr__
  • __eq__, __hash__ and NotImplemented
  • __add__, __radd__ and __lt__
  • __len__, __getitem__ and truthiness
  • Callable objects with __call__

Questions people ask

Do I need an account?

No. Anyone can play. Sign in to keep your XP, streak, badges and history.

Which Python version?

Python 3.12. Every question about output was checked by running the code.

Can I retake it?

Yes. Your best score is kept, and the questions and answers are shuffled each time.