LearnFrenzy
Sign in Take a free test

Inheritance and Polymorphism · Inheritance and Polymorphism MCQ · Question 12

Why __radd__? Answer, explanation & discussion

  • MCQ
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

Why does sum([Money(1), Money(2)]) need __radd__?

  • A sum starts with 0, so it computes 0 + Money(1), which needs Money.__radd__ Correct
  • B sum adds from right to left
  • C __add__ is ignored by sum
  • D It does not need it
Correct Answer: Option A

int.__add__(Money) returns NotImplemented, so Python falls back to Money.__radd__(0).

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links