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 trialAndrew McLane
3,385 PointsUnderstanding how methods in classes work.
Iļø only understand how all these methods work in a class. So all the methods run automatically and bounce code back and forth. Like does the dunder add method run, get an error, pass it to dunder radd and the. This passes t back to dunder add?
And if so, is this how all methods in a class run after being initialized?
6 Answers
Steven Parker
231,248 PointsSpecial ("magic") methods like "__add__
" run when you perform a certain operation on a member of the class. In this case, it would be when you add ("+") class instances together, or add another object to it.
When "__add__
" fails or is not present, it will try "__radd__
" to handle the operation. But in that case it will not pass anything back to "__add__
".
For more information, see Emulating Numeric Types in the Python reference documentation.
Andrew McLane
3,385 PointsSo all these methods run in order, down the line? So if an int can't be read, it will automatically move down to float(), etc?
Steven Parker
231,248 PointsOnly the method(s) that pertain to the operation being performed will be used, and only the reflected ("r" version) will be used if the primary one is not present or fails. For example, if two items are being added ("+") the "__add__
" and/or "__radd__
" will be used, but not those for other operations (like "__sub__
" for instance).
Andrew McLane
3,385 PointsSorry,
last question -
- I'm still not understanding why we have to define all these functions for float, str, int, etc. Before we were just able to use them as built in functions that python possessed. Why in a class do we have to do this?
Steven Parker
231,248 PointsDepending on what your class represents, the concept of of operations like "adding" them might not be handled as you would want by default, so you can create custom versions.
For example, if the class was "animal", Python might not know offhand how to add a "moose" and a "squirrel". But you can define how.
Andrew McLane
3,385 Pointsthanks!
Mohammed Ismail
7,190 PointsSteven Parker Hi, I have always loved your detailed explanation on questions/subject.
I am learning python and sometimes feel like I need to work more on the algorithms and logical reasoning.
Can you please suggest any reference links which will help improve my logic and understanding of python.
Thanks!
Steven Parker
231,248 PointsI got alerted by your tag.
Most everything I know about Python I learned right here at Treehouse!
My favorite go-to reference is the official documentation at Python.org.
Mohammed Ismail
7,190 Points@Steven Parker, Thanks!
Tyler Wells
Python Development Techdegree Student 2,678 PointsTyler Wells
Python Development Techdegree Student 2,678 PointsHi Steven Parker your answers are always great, so thanks for that! Just wondering in this specific case you said
__radd__
won't bounce back to__add__
, but it kind of does doesn't it? We've redefined within the class how the+
operator works by creating the__add__
method no? That's why we don't need the following code within__radd__
to make it work right?Because it uses the
+
operator which already includes that code? Or am I completely off base hereSteven Parker
231,248 PointsSteven Parker
231,248 PointsI was creating a distinction between "bounce back" (program flow) and "pass back" (returning a value).
And yes, you have defined what addition does with these objects, and you only need to code the operation once.