Chapter 10: Classes

Example 10.1, Page no: 233

In [1]:
class Ratio:
    def __init__(self):
        self.num = 0
        self.den = 0
    def assign(self,n,d):
        self.num = n
        self.den = d
    def convert(self):
        return float(self.num)/self.den
    def invert(self):
        self.num,self.den = self.den,self.num
    def print_(self):
        print self.num , '/' , self.den ,
    
x = Ratio()
x.assign(22,7)
print "x = ",
x.print_()
print " = " , x.convert() 
x.invert()
print  "1/x = ",
x.print_()
x =  22 / 7  =  3.14285714286
1/x =  7 / 22

Example 10.2, Page no: 234

In [2]:
class Ratio:
    def __init__(self):
        self.num = 0
        self.den = 0
    def assign(self,n,d):
        self.num = n
        self.den = d
    def convert(self):
        return float(self.num)/self.den
    def invert(self):
        self.num,self.den = self.den,self.num
    def print_(self):
        print self.num , '/' , self.den ,

Example 10.3, Page no: 235

In [3]:
class Ratio:
    def __init__(self,n,d):
        self.num = n
        self.den = d
    def print_(self):
        print self.num , '/' , self.den ,

x = Ratio(-1,3)
y = Ratio(22,7)
print "x = ",
x.print_()
print " and y = ",
y.print_()
x =  -1 / 3  and y =  22 / 7

Example 10.4, Page no: 236

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

x = Ratio()
y = Ratio(4)
z = Ratio(22,7)
print "x = ",
x.print_()
print "\ny = ",
y.print_()
print "\nz = ",
z.print_()
x =  0 / 1 
y =  4 / 1 
z =  22 / 7

Example 10.5, Page no: 237

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

Example 10.6, Page no: 237

In [6]:
class Ratio:
    def __init__(self,n=0,d=1):
        self.num = n
        self.den = d
    def print_(self):
        print self.num , '/' , self.den ,


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

Example 10.7, Page no: 238

In [7]:
class Ratio:
    def __init__(self,n=0,d=1):
        self.num = n
        self.den = d
    def numerator(self):
        return self.num
    def denominator(self):
        return self.den
    def print_(self):
        print self.num , '/' , self.den ,


x = Ratio(22,7)
print x.numerator() , '/' , x.denominator() 
22 / 7

Example 10.8, Page no: 238

In [8]:
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 numerator(self):
        return self.num
    def denominator(self):
        return self.den
    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(100,-360)
x.print_()
-5 / 18

Example 10.9, Page no: 240

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=None):
        if d == None:
            self.num = n.num
            self.den = n.den
        else:   
            self.num = n
            self.den = d
        self.reduce()
    def numerator(self):
        return self.num
    def denominator(self):
        return self.den
    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(100,360)
y = Ratio(x)
print "x = ",
x.print_()
print "y = ",
y.print_()
x =  5 / 18 y =  5 / 18

Example 10.10, Page no: 241

In [12]:
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=None):
        if d == None:
            print "COPY CONSTRUCTOR CALLED"
            self.num = n.num
            self.den = n.den
        else:   
            self.num = n
            self.den = d
        self.reduce()
    def numerator(self):
        return self.num
    def denominator(self):
        return self.den
    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

def f(r):
    s = Ratio(r)

x = Ratio(22,7)
y = Ratio(x) #calls the copy constructor, copying x to y
f(y)
COPY CONSTRUCTOR CALLED
COPY CONSTRUCTOR CALLED

Example 10.11, Page no: 242

In [13]:
'''
Note : Python objects die when program gets exit.
'''

class Ratio:
    def __init__(self):
        print "OBJECT IS BORN."
    def __del__(self):
        print "OBJECT DIES."

x =  Ratio()
print "Now x is alive."
print "Now between blocks."
y = Ratio()
print "Now y is alive."
OBJECT IS BORN.
Now x is alive.
Now between blocks.
OBJECT IS BORN.
Now y is alive.

Example 10.12, Page no: 244

In [14]:
class X:
    def __init(self):
        data = 0

p = X()
p.data = 22
print "p.data = " , p.data , " = " , p.data
p.data = 44
print " p.data = " , p.data , " = " , p.data 
p.data =  22  =  22
 p.data =  44  =  44

Example 10.13, Page no: 244

In [15]:
class Node:
    def __init__(self,d,q=None):
        self.data = d
        self.next = q

n = int(raw_input())
q = Node(n)
while True:
    n = int(raw_input())
    if n<=0:
        break
    p = Node(n, q)
    q = p
k = p
while k != None:
    print k.data , '->' , 
    k = k.next
print '*'
22
33
44
55
66
77
0
77 -> 66 -> 55 -> 44 -> 33 -> 22 -> *

Example 10.14, Page no: 246

In [16]:
'''
Python does not support static data type.
Python automatically handles local variable so we need not to delete it.
'''
count = 0
class Widget:
    def __init__(self):
        global count
        count += 1
        
w = Widget()
x = Widget()
print  "Now there are " , count , 'widgets'
if True:
    w = Widget()
    x = Widget()
    y = Widget()
    z = Widget()
    print "Now there are" , count , 'widgets' 
print "Now there are " , count , 'widgets'
y = Widget()
print "Now there are " , count , 'widgets'
OBJECT DIES.
Now there are  2 widgets
OBJECT DIES.
Now there are 6 widgets
Now there are  6 widgets
Now there are  7 widgets

Example 10.15, Page no: 246

In [17]:
count = 0
class Widget:
    def __init__(self):
        global count
        count += 1
    def numWidgets(self):
        global count
        return count
        
w = Widget()
x = Widget()
print  "Now there are " , w.numWidgets() , 'widgets'
if True:
    w = Widget()
    x = Widget()
    y = Widget()
    z = Widget()
    print "Now there are" , w.numWidgets() , 'widgets' 
print "Now there are " , w.numWidgets() , 'widgets'
y = Widget()
print "Now there are " , w.numWidgets() , 'widgets'
Now there are  2 widgets
Now there are 6 widgets
Now there are  6 widgets
Now there are  7 widgets

Example 10.16, Page no: 247

In [18]:
count = 0
class Widget:
    def __init__(self):
        global count
        count += 1
    def numWidgets(self):
        global count
        return count
        
w = Widget()
x = Widget()
print  "Now there are " , w.numWidgets() , 'widgets'
if True:
    w = Widget()
    x = Widget()
    y = Widget()
    z = Widget()
    print "Now there are" , w.numWidgets() , 'widgets' 
print "Now there are " , w.numWidgets() , 'widgets'
y = Widget()
print "Now there are " , w.numWidgets() , 'widgets'
Now there are  2 widgets
Now there are 6 widgets
Now there are  6 widgets
Now there are  7 widgets