Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Advanced Objects Math

Why 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
Justin Cox
12,122 Points

I 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!