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

Eric Peppler
Eric Peppler
3,600 Points

So confused

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?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse 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-int.

Another example, imagine a Bookshelf class, where the __add__ method added all the books from another Bookshelf class to its own inventory.

Some __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!

Eric Peppler
Eric Peppler
3,600 Points

Aha, I think I'm understanding better now.

"Another example, imagine a Bookshelf class, where the __add__ method added all the books from another Bookshelf class to its own inventory."

So if I defined the bookshelf class method __add__ to add their inventories together, I could something like book_shelf1 + book_shelf2 and I would receive a third bookshelf instance (or perhaps the first again, the way you phrased it) with both inventories now merged? Whereas, before, the interpreter would have no idea what the addition symbol was supposed to do there, correct?

[MOD: added backtick (`) to escape underscores -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 67,665 Points

Exactly.

>>> class Bookshelf:
...     pass
... 
>>> b1 = Bookshelf()
>>> b2 = Bookshelf()
>>> b1 + b2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'Bookshelf' and 'Bookshelf'

Alternatively:

>>> class Bookshelf2:
...     def __init__(self):
...         self.inventory = []
...     def __add__(self, other):
...         self.inventory.extend(other.inventory)
... 
>>> b1 = Bookshelf2()
>>> b2 = Bookshelf2()
>>> b1.inventory.append("The Lorax")
>>> b2.inventory.append("Zen and the Art of Motorcycle Maintenance")
>>> b1.inventory
['The Lorax']
>>> b1 + b2
>>> b2.inventory
['Zen and the Art of Motorcycle Maintenance']
>>> b1.inventory
['The Lorax', 'Zen and the Art of Motorcycle Maintenance']
Eric Peppler
Eric Peppler
3,600 Points

Niice, got it. Appreciate the help!