Review the Rational class definition (Fig. 8.9) and driver (Fig. 8.10). What happens when Python executes the following statement? x = 1 + Rational( 3, 4 ) Special methods __radd__, __rsub__ and so on overload the mathematical oper- ators for a user-defined class when an object of that class is used as the right-hand value of an operator. For each operator overloaded in Fig. 8.9 (i.e.,
operators +, -, *, / and //), add a corresponding method for overloading the operator when a Rational appears to the right of that operator.
What will be an ideal response?
```
# Definition of class Rational with right-hand operators.
def gcd( x, y ):
"""Computes greatest common divisor of two values"""
while y:
z = x
x = y
y = z % y
return x
class Rational:
"""Representation of rational number"""
def __init__( self, top = 1, bottom = 1 ):
"""Initializes Rational instance"""
# do not allow 0 denominator
if bottom == 0:
raise ZeroDivisionError, "Cannot have 0 denominator"
# assign attribute values
self.numerator = abs( top )
self.denominator = abs( bottom )
self.sign = ( top * bottom ) / ( self.numerator *
self.denominator )
self.simplify() # Rational represented in reduced form
def simplify( self ):
"""Simplifies a Rational number"""
common = gcd( self.numerator, self.denominator )
self.numerator /= common
self.denominator /= common
# overloaded unary operator
def __neg__( self ):
"""Overloaded negation operator"""
return Rational( -self.sign * self.numerator,
self.denominator )
# overloaded binary arithmetic operators
def __add__( self, other ):
"""Overloaded addition operator"""
return Rational(
self.sign * self.numerator * other.denominator +
other.sign * other.numerator * self.denominator,
self.denominator * other.denominator )
def __radd__( self, other ):
"""Overloaded right addition operator"""
return self.__add__( other )
def __sub__( self, other ):
"""Overloaded subtraction operator"""
return self + ( -other )
def __rsub__( self, other ):
"""Overloaded right subtraction operator"""
return other + ( -self )
def __mul__( self, other ):
"""Overloaded multiplication operator"""
return Rational( self.numerator * other.numerator,
self.sign * self.denominator *
other.sign * other.denominator )
def __rmul__( self, other ):
"""Overloaded right multiplication operator"""
return self.__mul__( other )
def __div__( self, other ):
"""Overloaded / division operator."""
return Rational( self.numerator * other.denominator,
self.sign * self.denominator *
other.sign * other.numerator )
def __rdiv__( self, other ):
"""Overloaded right / division operator."""
return Rational( other.numerator * self.denominator,
other.sign * other.denominator *
self.sign * self.numerator )
def __truediv__( self, other ):
"""Overloaded / division operator. (For use with
Python versions (2.2 or higher) that contain the
// operator)"""
return self.__div__( other )
def __rtruediv__( self, other ):
"""Overloaded right / division operator. (For use with
Python versions (2.2 or higher) that contain the
// operator)"""
return self.__rdiv__( other )
# overloaded binary comparison operators
def __eq__( self, other ):
"""Overloaded equality operator"""
return ( self - other ).numerator == 0
def __lt__( self, other ):
"""Overloaded less than operator"""
return ( self - other ).sign < 0
def __gt__( self, other ):
"""Overloaded greater than operator"""
return ( self - other ).sign > 0
# overloaded built-in functions
def __abs__( self ):
"""Overloaded built-in function abs"""
return Rational( self.numerator, self.denominator )
def __str__( self ):
"""String representation"""
# determine sign display
if self.sign == -1:
signString = "-"
else:
signString = ""
if self.numerator == 0:
return "0"
elif self.denominator == 1:
return "%s%d" % ( signString, self.numerator )
else:
return "%s%d/%d" % \
( signString, self.numerator, self.denominator )
# overloaded coercion capability
def __int__( self ):
"""Overloaded integer representation"""
return self.sign * divmod( self.numerator,
self.denominator )[ 0 ]
def __float__( self ):
"""Overloaded integer representation"""
return self.sign * float( self.numerator ) / self.denominator
def __coerce__( self, other ):
"""Overloaded coercion. Can only coerce int to Rational"""
if type( other ) == type( 1 ):
return ( self, Rational( other ) )
else:
return None
```
>>> x = 1 + Rational( 3, 4 )
>>> print x
7/4
>>> y = Rational( 2, 5 ) * Rational( 3, 7 )
>>> print y
6/35
>>> print Rational( 4, 3 ) / 4
1/3
>>> print 5 - Rational( 2, 5 )
23/5
You might also like to view...
On many computers a layer of __________, sometimes called firmware, lies between memory and the processor
a. cache memory b. microcode c. coprocessors d. none of the above
How do you configure alters with Intune?
What will be an ideal response?
SDLC stands for ________
Fill in the blank(s) with correct word
If a paragraph is associated with a picture, the paragraph text wraps around the space filled by the picture
Indicate whether the statement is true or false