Next / Previous / Contents / Shipman's homepage

3.10. Rational.__float__(): Implement the float() function

This method is called whenever Python's built-in float() function is called to convert an instance of the Rational class. To do this, we convert the numerator and the denominator to float type and then use a floating division.
    def __float__ ( self ):
        """Implement the float() conversion function.
        return  float ( self.n ) / float ( self.d )