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

hamsternation
hamsternation
24,689 Points

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 67,665 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!!!

hamsternation
hamsternation
24,689 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?

... 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 67,665 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".

hamsternation
hamsternation
24,689 Points

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... :'(