I made a class that can do some fraction arithmetic. I change the built-in method of __add__, __sub__,__mul__,and __div__ so that it can do arithmetic with fractions. I can use it with the +, - , * , / symbols.
My question is, what do I have to do to be able to use __iadd__ as +=.
class Fraction:
def __init__(self,num,den):
self.num = num
self.den = den
def __str__(self):
return str(self.num)+" / "+str(self.den)
def __add__(self,other):
num = self.num * other.den + other.num * self.den
den = self.den * other.den
common = self.gcf(num,den)
return Fraction(num/common , den/common)
def __iadd__(self,other):
self.num = self.num * other.den + other.num * self.den
self.den = self.den * other.den
common = self.gcf(self.num,self.den)
self.num = self.num/common
self.den = self.den/common
objectif you are using Python 2.x.