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 Object-Oriented Python Advanced Objects Math

usage of self.value versus self why are we using self inside add, radd methods instead self.value

why are we using self instead of self.value in add, radd, etc. how is self inside the radd working with out casting it to Int

why is my code failing when i write like this

 def __add__(self, other):
       return int(self) + other
    '''
    why cant we write like this what is the difference 
    between self and self.value in this context
    '''
       #return int(self.value) + other 

def __radd__(self, other):
    return self + other
    '''
    why cant we write like this
    '''
    #return int(self.value) + other

2 Answers

Steven Parker
Steven Parker
231,275 Points

You don't need to access the "value" property because the built-in conversion processes do it for you when you perform an operation directly on a class instance.

And you don't want to use explicit "int" conversion, since the values might be floating point, and the built-in conversions will take care of that if you allow them to do their job.

Hank Tao
Hank Tao
7,540 Points

int(self) only works when you have def int(self): return int(self.value)

If not then only int(self.value) would work