Next / Previous / Contents / Shipman's homepage

3.6. Rational.__sub__(): Implement subtraction

This method is called when a Rational instance appears on the left side of the “-” operator. The right-hand operand must be a Rational instance as well. See Section 3.5, “Rational.__add__(): Implement the addition (+) operator” for the algebra of this operation.

rational.py
    def __sub__ ( self, other ):
        """Return self minus other.
        """
        return Rational ( self.n * other.d - other.n * self.d,
                          self.d * other.d )