First page Back Continue Last page Overview Graphics

Improved __add__ and __iadd__

def __add__(self, other):

if type(other) == int or type(other) == float:

l = self.convert2metres() + other

else:

l = self.convert2metres() + other.convert2metres()

return Length(l / Length.__metric[self.unit], self.unit )

def __iadd__(self, other):

if type(other) == int or type(other) == float:

l = self.convert2metres() + other

else:

l = self.convert2metres() + other.convert2metres()

self.value = l / Length.__metric[self.unit]

return self

Now, using the class is more convenient:

z = Length(4.5, "yd") + 1

x += 5