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 trial

Python

Question About In Place Magic Methods

For in place magic methods, at least in the answers for some of the exercises and in the videos, when defining one of these methods to update a value we use the expression 'self.value = self + other'. My question is why do we use self rather than 'self.value = self.value + other'. I do not understand why putting self there works.

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

I like that you are thinking about this-- it is kind of an advanced topic and a little bit dependent on what the code designer is looking to achieve. Personally I favor methods that don't coerce or change the data type.

Rather than reinventing the answer, Chris Freeman (who is an excellent moderator) answered it this way:

https://teamtreehouse.com/community/iadd-changes-the-data-type