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 trialhamsternation
26,616 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,441 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,616 Pointshamsternation
26,616 PointsAre you saying all the
__radd__
does is catch the reflected add call, while thereturn 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 toNumString(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:
where it's not always the case that
__radd__
returnsself + other
?Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 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 + 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
26,616 Pointshamsternation
26,616 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 + 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 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... :'(