a*c/b*d in a new object ''' retval = Rational(self.num * rhs.num, self.den * rhs.den) # create a new object return retval def __add__(self, rhs): # this special method is called when * operator is used ''' (a/b)+(c/d) -> a*d+b*c/d*b in a new object ''' retval = Rational(self.num * rhs.den + rhs.num * self.den, self.den * rhs.den) # create a new object with sum return retval # -, /, and other operators left as exercise def __eq__(self, rhs): # called when == operator is used '''a*d == b*c ''' return self.num*rhs.den == self.den*rhs.num def __lt__(self, rhs): # called when < operator is used '''a*d < b*c ''' return self.num*rhs.den < self.den*rhs.num # rest can be defined in terms of the first two def __ne__(self, rhs): return not self == rhs def __le__(self, rhs): return self < rhs or self == rhs def __gt__(self, rhs): return not self <= rhs def __ge__(self, rhs): return not self < rhs .. code:: python # Let us play with our Rational class a = Rational(3, 9) b = Rational(16, 24) print(a, b, a*b+b*a) print(a