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 trialHenry Lin
11,636 Pointswhy 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]
3 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsYes, both int(self)
and 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 self.__int__
returns int(self.value)
. In either case, the built-in int
will eventually call the __int__
method on whatever object is stored in value
.
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.
Philip Schultz
11,437 PointsHello Chris, Is it best practice to use the shortcut? What would happen if you added another attribute to the class? You would have to specifically say self.value, right? Does this only work (using int(self)) because the class only consists of one attribute?
Chris Freeman
Treehouse Moderator 68,457 PointsHi Philip! I saw your other post on this challenge and answered it there. I've also updated the answer above based on my answer to your questions.
TL;DR: outside of the __int__
method, it is best to use int(self)
not int(self.value)
so that the __int__
method is the one-stop-shop for converting to an integer for the class.
Chris Freeman
Treehouse Moderator 68,457 PointsSorry Philip, I had a typo in the this post link. Fixed above.
ahsansalem
3,696 PointsI know Mr. Freeman answered the question correctly but I found the same issue that Henry found. In the start of the video, the __int__
and __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 __int__
, and __float__
are compatible.
[MOD: added ` formatting for dunders -cf]
ahsansalem
3,696 PointsI 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]
Chris Freeman
Treehouse Moderator 68,457 PointsIn this extension, the addition will end up call str.__add__
method because there is a str
on the left of the plus sign. This will result in concatenating the strings.
nicole lumpkin
Courses Plus Student 5,328 Pointsnicole lumpkin
Courses Plus Student 5,328 PointsGreat question Henry Lin !!!