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 trialTyun Chan
Python Web Development Techdegree Student 1,464 Pointsabout return 'self' and 'self.value'
Why add(), radd() and iadd() return 'self' instead of 'self.value'? What's different between these method and str() or float(), whose return value is self.value?
1 Answer
Steven Parker
231,275 PointsThe methods __add__
and __radd__
don't return "self" but a calculation involving "self". An implicit numeric conversion during the calculation will call either __int__
or __float__
, both of which return "self.value" converted to a number.
On the other hand, __iadd__
does return "self.value".