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 trialHemin mohammed
Python Development Techdegree Student 1,424 Pointsi am really confused about this magic functions "I have written my questions as comments"
class NumString: def init(self, value): self.value = str(value)
def __str__(self):
return self.value
def __int__(self):
return int(self.value)
# why here we don't write float(self) what is difference
def __float__(self):
return float(self.value)
def __add__(self, other):
if '.' in self.value:
return float(self) + other
return int(self) + other
def __radd__(self, other):
return self + other
# what self represents and what self.value represent below (self.value =self+other)
def __iadd__(self, other):
self.value = self + other
return self.value
class NumString:
def __init__(self, value):
self.value = str(value)
def __str__(self):
return self.value
def __int__(self):
return int(self.value)
# why here we don't write float(self) what is difference
def __float__(self):
return float(self.value)
def __add__(self, other):
if '.' in self.value:
return float(self) + other
return int(self) + other
def __radd__(self, other):
return self + other
# what self represents and what self.value represent below (self.value =self+other)
def __iadd__(self, other):
self.value = self + other
return self.value
1 Answer
Jeff Muday
Treehouse Moderator 28,722 PointsThose are some very good questions to ask!
This particular NumString class is good for working with numbers that start as strings but can be treated as a float or and int type. I have created classes like this to work on GeoCoding applications of latitude/longitude which are just as likely represented as strings from some types of GPS loggers or numbers. If you are interested, here are a few of the different types (http://www.geomidpoint.com/latlon.html). My GeoCode type/class is a little more complex in that it will see any one of the representations and convert it accordingly to "signed degrees" format on which actual computations can take place. NumString is kind of like that too.
Question: "why here we don't write float(self) what is difference"
comment 1 - you definitely could write float(self), it would not be quite as useful-- ideally, we want to use a NumString interchangeably with any other number type. **Specifically, we use the __float__() method because it allows the type to use the generic method float() and addition which was defined
pretty handy!
>>> x = NumString('3.14')
>>> x
<__main__.NumString object at 0x0000021F19A3F370>
>>> x.__float__()
3.14
>>> # look, we can use type coercion correctly!
>>> float(x)
3.14
>>> # notice when adding the int type we get a tiny bit of rounding artifact.
>>> x + 1
4.140000000000001
>>> x + 2.2
5.34
Question: "what does "self" represent and what does self.value represent below (self.value =self+other)"
To begin answering this, we need to know what __iadd()__ does.
The __iadd()__ method is different in that it makes a computation on its own value and the value of another expression and stores it back into the self.value. "self" is the way an object refers to its own methods and properties. So the iadd returns the RESULT of the addition computation and stores it into the variable name. (Later on you will appreciate the difference between mutable and immutable types, and this is an example of immutable -- if you don't know what the means, it's ok, you'll learn it later.)
I am not a fan of using that function on the NumString type because it has a side effect of turning x into a numeric type-- computationally this is fine, but it isn't a good example of functional programming.
See below:
>>> x = NumString('12.2')
>>> x
<__main__.NumString instance at 0x7f6f4aefafa0>
>>> x.value
'12.2'
>>> x + 2
14.2
>>> # here is where __iadd__() is called (below). Computationally fine, but see the side effect
>>> x += 2
>>> x
14.2
>>> # now, what about the side effect-- x is now a float!
>>> type(x)
<type 'float'>