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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Stephen Cole](https://secure.gravatar.com/avatar/c318b8e289f72d3e9b157df9785c598a?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Stephen Cole
Courses Plus Student 15,809 PointsWhy doesn't __radd__ need to be explicitly coded? Is it because, by adding, it invokes the __add__ method?
I tested it and it works.
Kenneth says that we should do the "right thing" but I don't understand why the "right thing" works.
2 Answers
![Chris Freeman](https://uploads.teamtreehouse.com/production/profile-photos/1153532/micro_image.jpg)
Chris Freeman
Treehouse Moderator 68,425 Points- If you have
some_object + 4
thensome_object.__add__()
is called. - If you have
4 + some_object
, thensome_object.__radd__()
is called
This is done because objects might behave differently depending on which side of the plus-sign they're on.
If symmetry exists, the having __radd__
call __add__
is a quick solution.
![Stephen Cole](https://secure.gravatar.com/avatar/c318b8e289f72d3e9b157df9785c598a?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Stephen Cole
Courses Plus Student 15,809 PointsRight answer, wrong question.
I know/understand that __radd__
provides for reflective addition.
However, when overriding the __add__
method, I had to define how the addition worked. That is, if there was a period ('.') in the string, make it a float, otherwise, make it an int.
Why is it that I do not have to do the same thing with __radd__
?
Is it because __radd__
uses __add__
?
Is it because it assumes the type after it gets the first number?
Is there some other magic behind the scenes?
[MOD: added ` around build-in names to protect from formatting as bold]
Chris Freeman
Treehouse Moderator 68,425 PointsChris Freeman
Treehouse Moderator 68,425 PointsExpanding answer:
There are two main choices,:
__radd__
as is in__add__
(this is not DRY), or__add__
to have it do the work for__radd__
.So, yes, It is because
__radd__
uses__add__
.