Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialben127127
1,980 PointsWhy does the __add__ not work if you enter the NumString second?
Referenced around 3:23 in the video 'Math' with Kenneth Love. I don't get why it stops working when instead of saying NumString(2) + 2 you do 2 + NumString(2)
1 Answer
Justin Cox
12,123 PointsI haven't watched that video in a while, but if I remember correctly you will need to use __radd__
to achieve what you're looking for. Python will first check to see if __add__
will evaluate, if not it checks to see if the right-hand operand has an __radd__
method implemented, and will instead try to evaluate that.
For a better explanation why , click here to check out Rhomboid's answer over on Reddit
It should be the top answer on there. I hope this helps!
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsMoved comment to answer. More info here: radd reference docs