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

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
210,423 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