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.
Eric Peppler3,600 Points
I've been doing this for awhile now and this is the most confused I've been yet...what is the point of what he's doing? I've watched the video 3 times and I still have no clue what I'm even watching. Instead of age = 25, age+5=30...he's defining a class and within the class he's defining a method that adds? Then he's creating a NumString instance, which ends up as an int or float...and adding it to an int? What am I supposed to be taking away from this lesson?
Chris FreemanTreehouse Moderator 67,665 Points
The takeaway is not obvious. It's probably "if you create a class that you wish to use math on its instances, you'll need to create these math handling methods". This specific example of a string that can be operated upon with addition is as basic one can make. The idea being to show a simple non-math object that can be added.
Normal strings can be also added together, but only to other strings. Their
__add__ results in a new string through concatenation. Strings can be multiplied, but the
__mul__ method raises an error if the "other" is a non-
Another example, imagine a
Bookshelf class, where the
__add__ method added all the books from another
Bookshelf class to its own inventory.
__add__ methods add steps to do type checking and only allow adding to same type objects.
Post back if you need more help. Good Luck!