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 trial

Python Object-Oriented Python Advanced Objects Math

Not getting the __radd__ part of the code

For the __add__ method, he uses the if statement to test for float or int properties, why doesn't he need to do it again for the __radd__? I'm not getting the explanation from the video. :\

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

He avoids retesting for floats/ints in __radd__ by passing the problem on to __add__. All __radd__ has to do is receive and pass along the arguments.

Post back if you need more help. Good luck!!!

Are you saying all the __radd__ does is catch the reflected add call, while the return self + other portion of the __radd__ passes argument directly to the __add__ method?

... or is he saying that the return self + other passes the argument back to NumString(2) + 2 at which point, the __add___ method is called?

and when he says this isn't always the case... would the following be an example, where ... let's say you want to return a single title cased string whenever adding the ProperStrings class:

class ProperStrings:
    def __init__(self, value):
        self.value = str(value)
    def __add__(self, other):
        return self.value.title() + other.lower()
    def __radd__(self, other):
        return other.title() + self.value.lower()

where it's not always the case that __radd__ returns self + other?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Are you saying all the radd does is catch the reflected add call, while the return self + other portion of the radd passes argument directly to the add method? Yes, exactly.

Normally, the __radd__ would return self + other so that the "self" is now on the left-side of the plus sign. This would then automatically trigger __add__ since we are now adding "self" again.

In your ProperStrings class, the behavior is slightly different. Since the __radd__ is returning a .title() + .title(), these both result in strings so the plus sign going to cause calling the string __add__ method. It only stays in this class's methods if one of the operands is "self".

Oh okay, I get it now. Thanks, Chris!

I guess for my ProperStrings example, I was trying to give an example of Kenneth's comment that return self + other might not be appropriate for every __radd__ method. Here what I meant to type was that since ProperStrings returns a title cased concatenated string every time, the __radd__ wouldn't work with simply return self + other

:D I get it now though. dang I can't wait to play around with it. I'm sure this is only the basic of the basics... :'(