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

Python OOP

class NumString:
    def __init__(self, value):
        self.value = str(value)

    def __str__(self):
        print('__str__ ran')
        return self.value

    def __int__(self):
        print('__int__ ran')
        return int(self.value)

    def __float__(self):
        print('__float__ ran')
        return float(self.value)

    def __add__(self, other):
        print('__add__ ran')
        if '.' in self.value:
            return float(self) + other
        return int(self) + other

    def __radd__(self, other):
        print('__radd__ ran')
        return self + other

    def __iadd__(self, other):
        print('__iadd__ ran')
        self.value = self + other
        return self.value

a = NumString('10.5').__add__(10)
print(isinstance(a, float))

# When I run the code I get:
#   __add__ ran <--- Makes sense to get this back
#   __float__ ran <--- I don't know why I got this back. I didn't call the __float__
#   True <--- Makes sense

# But when I change the value to "10"(a = NumString('10').__add__(10)), I get this back
#   __add__ ran <--- makes sense
#   __int__ ran <--- why ;/
#   False <--- makes sense

# So why did __float__ and __int__ showed up when I did not even call them?
# Is __float__ and float()  && __int__ and int() the same thing but just written differently?

1 Answer

# When I run the code I get:
#   __add__ ran <--- Makes sense to get this back
#   __float__ ran <--- I don't know why I got this back. I didn't call the __float__
#   True <--- Makes sense

# The output you get that says __float__ ran is because this method overrides what is called
# when float() is called. your  __add__ does call __float__ if the string has a period in it.

# But when I change the value to "10"(a = NumString('10').__add__(10)), I get this back
#   __add__ ran <--- makes sense
#   __int__ ran <--- why ;/
#   False <--- makes sense

# This is the same thing. When the number has no period in it, it changes it to 
# an int. __init__ is called by your __add__ function

# dunder functions such as __add__, __float__, and __int__ overide the functions for 
# objects created by the class they are in. __add__ overides how the + operator works,
#  __int__ overides how int() function works and __float__ changes how the float() function works

# For example, because of this when you declare the following...
a = NumString(10.5)

# You can add the NumString to a number by doing this:
answer = a + 5

Thank you, now it makes sense!