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
hamsternation
26,617 PointsNot 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
Treehouse Moderator 68,468 PointsHe 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
26,617 Pointshamsternation
26,617 PointsAre you saying all the
__radd__does is catch the reflected add call, while thereturn self + otherportion of the__radd__passes argument directly to the__add__method?... or is he saying that the
return self + otherpasses the argument back toNumString(2) + 2at 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:
where it's not always the case that
__radd__returnsself + other?Chris Freeman
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsAre 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 returnself + otherso 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
ProperStringsclass, 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
26,617 Pointshamsternation
26,617 PointsOh 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 + othermight 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 simplyreturn 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... :'(