I want my Fraction
class to work as a float when it's being added to floats or integers so I can naturally perform operations with it, but it's only working when the Fraction
is the rightmost operand. Is there a way to make it work with the operands in any order or should I override another method that I haven't learned of?
Code (I guess variable names are pretty self-explanatory):
def __radd__(self,target):
if type(target) == int or type(target) == float:
return target + self.num/self.den
1 + Fraction(1,2)
returns 1.5
as it should but Fraction(1,2) + 1
raises:
Traceback (most recent call last):
File "/Users/mac/Desktop/programming/python/fraction.py", line 86, in <module>
print(my_fraction + 1)
File "/Users/mac/Desktop/programming/python/fraction.py", line 28, in __add__
new_den = self.den * target.den
AttributeError: 'int' object has no attribute 'den'