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.

Python Object-Oriented Python Advanced Objects Math

Henry Lin
Henry Lin
11,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]

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,665 Points

Yes, 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
Philip Schultz
11,343 Points

Hello 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
Chris Freeman
Treehouse Moderator 67,665 Points

Hi 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
Chris Freeman
Treehouse Moderator 67,665 Points

Sorry Philip, I had a typo in the this post link. Fixed above.

I 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]

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]

Chris Freeman
Chris Freeman
Treehouse Moderator 67,665 Points

In 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.