# Chapter 11: Overloading Operators¶

## Example 11.1, Page no: 256¶

In [1]:
class Ratio:
def __init__(self,n=None,d=None):
if d==None:
self.num = n.num
self.den = n.den
elif n==None:
self.num = 0
self.den = 1
else:
self.num = n
self.den = d

def equals(self):
return self  # retuns calling object.


## Example 11.2, Page no: 257¶

In [2]:
class Ratio:
def __init__(self,n=None,d=None):
pass

def equals(self):
pass


## Example 11.3, Page no: 256¶

In [3]:
class Ratio:
def __init__(self,n=None,d=None):
if d==None:
self.num = n.num
self.den = n.den
elif n==None:
self.num = 0
self.den = 1
else:
self.num = n
self.den = d

z = Ratio(22,7)
y = z
x = z

x = Ratio(22,7)
y = Ratio(x)
z = x
w = x


## Example 11.4, Page no: 259¶

In [4]:
class Ratio:
def __init__(self,n=None,d=None):
self.num = n
self.den = d
def __mul__(self,y):
pass


## Example 11.5, Page no: 259¶

In [5]:
def gcd(m,n):
# returns the greatest common divisor of m and n:
if (m<n):
m,n = n,m
while (n>0):
r = m % n
m = n
n = r
return m

class Ratio:
def __init__(self,n=None,d=None):
self.num = n
self.den = d
self.reduce()
def __mul__(self,y):
z = Ratio(self.num * y.num, self.den * y.den)
return z
def print_(self):
print self.num , '/', self.den

def reduce(self):
# enforce invariant(den > 0):
if (self.num == 0 or self.den == 0):
self.num = 0
self.den = 1
return
if (self.den < 0):
self.den *= -1
self.num *= -1
# enforce invariant(gcd(num,den) == 1):
if (self.den == 1):
return
# it's already reduced
sgn = 0
if self.num < 0:
sgn = -1
else:
sgn = 1
g = gcd(sgn*self.num,self.den)
self.num /= g
self.den /= g
x = Ratio(22,7)
y = Ratio(-3,8)
z = x
z.print_()
x = y*z
x.print_()

22 / 7
-33 / 28


## Example 11.6, Page no: 260¶

In [6]:
def gcd(m,n):
# returns the greatest common divisor of m and n:
if (m<n):
m,n = n,m
while (n>0):
r = m % n
m = n
n = r
return m

class Ratio:
def __init__(self,n=None,d=None):
self.num = n
self.den = d
self.reduce()
def __imul__(self,y):
self.num = self.num * y.num
self.den = self.den * y.den
def print_(self):
print self.num , '/', self.den


## Example 11.7, Page no: 260¶

In [7]:
class Ratio:
def __init__(self,n=None,d=None):
self.num = n
self.den = d
self.reduce()
def __imul__(self,y):
self.num = self.num * y.num
self.den = self.den * y.den
def __eq__(self,y):
return (x.num * y.den == y.num * x.den)
def print_(self):
print self.num , '/', self.den


## Example 11.8, Page no: 261¶

In [8]:
'''
Python does not use << operator for printing. So here we are just declaring function name as print_.
'''

class Ratio:
def __init__(self,n=None,d=None):
self.num = n
self.den = d
def __imul__(self,y):
self.num = self.num * y.num
self.den = self.den * y.den
def __eq__(self,y):
return (x.num * y.den == y.num * x.den)
def print_(self):
print self.num , '/', self.den

x = Ratio(22,7)
y = Ratio(-3,8)
x.print_() ,  y.print_()

22 / 7
-3 / 8

Out[8]:
(None, None)

## Example 11.9, Page no: 262¶

In [1]:
'''
Python does not have >> for input. so we will use input function.
'''
def gcd(m,n):
# returns the greatest common divisor of m and n:
if (m<n):
m,n = n,m
while (n>0):
r = m % n
m = n
n = r
return m
class Ratio:
def __init__(self,n=0,d=1):
self.num = n
self.den = d
self.reduce()
def __mul__(self,y):
z = Ratio(self.num * y.num, self.den * y.den)
return z
def print_(self):
print self.num , '/', self.den

def input(self):
self.num = int(raw_input('Numerator : '))
self.den = int(raw_input('Denominator : '))
self.reduce()
def reduce(self):
# enforce invariant(den > 0):
if (self.num == 0 or self.den == 0):
self.num = 0
self.den = 1
return
if (self.den < 0):
self.den *= -1
self.num *= -1
# enforce invariant(gcd(num,den) == 1):
if (self.den == 1):
return
# it's already reduced
sgn = 0
if self.num < 0:
sgn = -1
else:
sgn = 1
g = gcd(sgn*self.num,self.den)
self.num /= g
self.den /= g

x = Ratio()
y = Ratio()
x.input()
y.input()
x.print_()
y.print_()

Numerator : -10
Denominator : -24
Numerator : 36
Denominator : -20
5 / 12
-9 / 5


## Example 11.10, Page no: 263¶

In [3]:
def gcd(m,n):
# returns the greatest common divisor of m and n:
if (m<n):
m,n = n,m
while (n>0):
r = m % n
m = n
n = r
return m
class Ratio:
def __init__(self,n=0,d=1):
self.num = n
self.den = d
self.reduce()
def __mul__(self,y):
z = Ratio(self.num * y.num, self.den * y.den)
return z
def print_(self):
print self.num , '/', self.den

def input(self):
self.num = int(raw_input('Numerator : '))
self.den = int(raw_input('Denominator : '))
self.reduce()
def __float__(self):
return float(self.num)/self.den
def reduce(self):
# enforce invariant(den > 0):
if (self.num == 0 or self.den == 0):
self.num = 0
self.den = 1
return
if (self.den < 0):
self.den *= -1
self.num *= -1
# enforce invariant(gcd(num,den) == 1):
if (self.den == 1):
return
# it's already reduced
sgn = 0
if self.num < 0:
sgn = -1
else:
sgn = 15.py
g = gcd(sgn*self.num,self.den)
self.num /= g
self.den /= g

x = Ratio(-5,8)
print "x = " ,
x.print_()
print ", float(x) = " , float(x)
P = Ratio(22,7)
PI = float(P)
print "P = " ,
P.print_()
print ", PI = " , PI

x =  -5 / 8
, float(x) =  -0.625
P =  22 / 7
, PI =  3.14285714286


## Example 11.11, Page no: 264¶

In [5]:
def gcd(m,n):
# returns the greatest common divisor of m and n:
if (m<n):
m,n = n,m
while (n>0):
r = m % n
m = n
n = r
return m
class Ratio:
def __init__(self,n=0,d=1):
self.num = n
self.den = d
self.reduce()
def __mul__(self,y):
z = Ratio(self.num * y.num, self.den * y.den)
return z
def print_(self):
print self.num , '/', self.den

def __iadd__(self,n):
self.num += self.den
return self
def reduce(self):
# enforce invariant(den > 0):
if (self.num == 0 or self.den == 0):
self.num = 0
self.den = 1
return
if (self.den < 0):
self.den *= -1
self.num *= -1
# enforce invariant(gcd(num,den) == 1):
if (self.den == 1):
return
# it's already reduced
sgn = 0
if self.num < 0:
sgn = -1
else:
sgn = 1
g = gcd(sgn*self.num,self.den)
self.num /= g
self.den /= g

x = Ratio(22,7)
x += 1
y = x
print "y = " ,
y.print_()
print ", x = ",
x.print_()

y =  29 / 7
, x =  29 / 7


## Example 11.12, Page no: 265¶

In [9]:
def gcd(m,n):
# returns the greatest common divisor of m and n:
if (m<n):
m,n = n,m
while (n>0):
r = m % n
m = n
n = r
return m
class Ratio:
def __init__(self,n=0,d=1):
self.num = n
self.den = d
self.reduce()
def __mul__(self,y):
z = Ratio(self.num * y.num, self.den * y.den)
return z
def print_(self):
print self.num , '/', self.den

def __iadd__(self,n):
self.num += self.den
return self
def reduce(self):
# enforce invariant(den > 0):
if (self.num == 0 or self.den == 0):
self.num = 0
self.den = 1
return
if (self.den < 0):
self.den *= -1
self.num *= -1
# enforce invariant(gcd(num,den) == 1):
if (self.den == 1):
return
# it's already reduced
sgn = 0
if self.num < 0:
sgn = -1
else:
sgn = 1
g = gcd(sgn*self.num,self.den)
self.num /= g
self.den /= g

x = Ratio(22,7)
y = Ratio(x.num,x.den)
x += 1
print "y = " ,
y.print_()
print ", x = ",
x.print_()

y =  22 / 7
, x =  29 / 7


## Example 11.13, Page no:266¶

In [7]:
def gcd(m,n):
# returns the greatest common divisor of m and n:
if (m<n):
m,n = n,m
while (n>0):
r = m % n
m = n
n = r
return m
class Ratio:
def __init__(self,n=0,d=1):
self.num = n
self.den = d
self.reduce()
def __mul__(self,y):
z = Ratio(self.num * y.num, self.den * y.den)
return z
def print_(self):
print self.num , '/', self.den

def __getitem__(self,k):
if k == 1:
return  self.num
else:
return self.den
def reduce(self):
# enforce invariant(den > 0):
if (self.num == 0 or self.den == 0):
self.num = 0
self.den = 1
return
if (self.den < 0):
self.den *= -1
self.num *= -1
# enforce invariant(gcd(num,den) == 1):
if (self.den == 1):
return
# it's already reduced
sgn = 0
if self.num < 0:
sgn = -1
else:
sgn = 1
g = gcd(sgn*self.num,self.den)
self.num /= g
self.den /= g

x = Ratio(22,7)
print "x = " ,
x.print_()
print "x[1] = " , x[1] , ", x[2] = " , x[2]

x =  22 / 7
x[1] =  22 , x[2] =  7