Return a new instance of MixedNumber
instead:
return MixedNumber(whole, remainder)
or, to make it more sub-class-friendly, of type(self)
:
return type(self)(whole, remainder)
Now your code prints:
>>> x = MixedNumber(3, Fraction(1, 3))
>>> y = MixedNumber(-1, Fraction(2, 5))
>>> z = x + y
>>> print z
MixedNumber(2, Fraction(11, 15)
>>> print repr(z)
MixedNumber(2, Fraction(11, 15)
but you could print the old result of __add__
explicitly:
>>> print '{} and {}'.format(z.whole_number, z.fraction)
2 and 11/15
You could make that your __str__
method:
def __str__(self):
return '{} and {}'.format(z.whole_number, z.fraction)
after which your print
statements would be:
>>> print z
2 and 11/15
>>> print repr(z)
MixedNumber(2, Fraction(11, 15)