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.
Henry Lin11,636 Points
why did the __add__ and __radd__ functions return float(self)/int (self) + other instead of float(self.value) + other?
The class has an init function which takes a single parameter and returns a string representation of that value. Therefore, when we create an instance of NumString(), it is an instance that has the attribute of 'value' that we passed in. Thus, 'self' keyword refers to the instance we create, and from what we learned previously, we should use self.function/attribute to invoke the element we want to use. So I think we should use float/int (self.value) in
__add__function. However, Kenneth, in the video, used just self alone. I really want to know the reason that he just used self here. By the way, I have tried self.value in
__add__ function and it worked fine.
[MOD: added ` escape formatting around dunders -cf]
Chris FreemanTreehouse Moderator 67,665 Points
int(self.value) work, The shortcut works because when
self is used in an
int context, such as,
int(self), before the built-in command
int is run,
self.__int__() is called to get the "int context of self". It can be seen at the start of the video that
int(self.value). In either case, the built-in
int will eventually call the
__int__ method on whatever object is stored in
It's a bit twisted, but it works. Post back if you have more questions!
EDIT: after thinking more about it. Using
int(self) is the correct approach. It's not really a "shortcut". While
int(self.value) would work, it is effectively bypasses using
__int__ to do the conversion it was written to explicitly do. This violates the DRY principle and my introduce errors in cases when an objects conversion to a numeric context is more complicated.
I go into way more detail in answering this post regarding the same challenge.
I know Mr. Freeman answered the question correctly but I found the same issue that Henry found. In the start of the video, the
__float__ both were using "self.value" only, but the
__add__ method was using "self" only without the attribute. So, we have to make sure the add method and the
__float__ are compatible.
[MOD: added ` formatting for dunders -cf]
I was playing more with the code and I wanted to add one more if statement to the add function to be able to accept the addition of string, int or float without drilling down to the attribute. I know it is a nonsense operation, but that's what we do when we have extra time :)
if type(other) is str: return str(self.value) + other
[MOD: added ```python formatting -cf]