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

Understanding 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
Steven Parker
210,475 Points

Special ("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.

Hi 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?

if '.' in self.value:
            return float(self) + other
        else:
            return int(self) + other

Because it uses the + operator which already includes that code? Or am I completely off base here

Steven Parker
Steven Parker
210,475 Points

I 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.

So 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
Steven Parker
210,475 Points

Only 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).

Sorry,

last question -

  1. 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
Steven Parker
210,475 Points

Depending 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.

thanks!

Steven 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
Steven Parker
210,475 Points

:bell: I 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.

@Steven Parker, Thanks!