Chapter 16: Templates

Example 16.1, Page Number: 376

In [1]:
def swapargs(a,b):
    temp=a[0]
    a[0]=b[0]
    b[0]=temp

#Variable declaration
i=[10]
j=[20]
x=[10.1]
y=[23.3]
a=['x']
b=['z']

print "Original i, j: ",i[0],j[0]
print "Original x,y: ",x[0],y[0]
print "Original a,b: ",a[0],b[0]

swapargs(i,j)        #swap integers
swapargs(x,y)        #swap floats
swapargs(a,b)        #swap chars

#Result
print "Swapped i, j: ",i[0],j[0]
print "Swapped x,y: ",x[0],y[0]
print "Swapped a,b: ",a[0],b[0]
Original i, j:  10 20
Original x,y:  10.1 23.3
Original a,b:  x z
Swapped i, j:  20 10
Swapped x,y:  23.3 10.1
Swapped a,b:  z x

Example 16.2, Page Number: 378

In [3]:
def myfunc(x,y):
    print x,y
    
myfunc(10,"hi")
myfunc(0.23,10L)
10 hi
0.23 10

Example 16.3, Page Number: 379

In [2]:
def swapargs(a,b):
    if isinstance(a[0],int):  #integer version
        temp=a[0]
        a[0]=b[0]
        b[0]=temp
        print "Inside swapargs int specialization."
    else:                  #generic version
        temp=a[0]
        a[0]=b[0]
        b[0]=temp
        print "Inside template swapargs."

#Variable declaration
i=[10]
j=[20]
x=[10.1]
y=[23.3]
a=['x']
b=['z']

print "Original i, j: ",i[0],j[0]
print "Original x,y: ",x[0],y[0]
print "Original a,b: ",a[0],b[0]

swapargs(i,j)        #calls explicitly overloaded swapargs()
swapargs(x,y)        #calls generic swapargs()
swapargs(a,b)        #calls generic swapargs()

#Result
print "Swapped i, j: ",i[0],j[0]
print "Swapped x,y: ",x[0],y[0]
print "Swapped a,b: ",a[0],b[0]
Original i, j:  10 20
Original x,y:  10.1 23.3
Original a,b:  x z
Inside swapargs int specialization.
Inside template swapargs.
Inside template swapargs.
Swapped i, j:  20 10
Swapped x,y:  23.3 10.1
Swapped a,b:  z x

Example 16.4, Page Number: 381

In [4]:
def f(a,b=None):
    if(b==None):                 #First version of f()
        print "Inside f(X a)"
    else:                        #Second version of f()
        print "Inside f(X a, Y b)"
        
f(10)      #calls f(X)
f(10,20)   #calls f(X,Y)
Inside f(X a)
Inside f(X a, Y b)

Example 16.5, Page Number: 382

In [5]:
 
def repeat(data,times):
    while times:
        print data
        times-=1
        
repeat("This is a test",3)
repeat(100,5)
repeat(99.0/2, 4)
This is a test
This is a test
This is a test
100
100
100
100
100
49.5
49.5
49.5
49.5

Example 16.6, Page Number: 383

In [6]:
def myabs(val):
    if  val<0:
        return -val
    else:
        return val

#Result
print myabs(-10)
print myabs(-10.0)
print myabs(-10L)
print myabs(-10.0)
10
10.0
10
10.0

Example 16.7, Page Number: 385

In [7]:
def SIZE():
    return 100
class queue:
    def __init__(self):
        self.q=[]
        self.sloc=self.rloc=0
    #Put an object into the queue
    def qput(self,i):
        if self.sloc==SIZE():
            print "Queue is full."
            return
        self.sloc+=1
        self.q.append(i)
    #Get an object from the queue.
    def qget(self):
        if self.rloc==self.sloc:
            print "Queue Underflow."
            return
        a=self.rloc
        self.rloc+=1
        return self.q[a]
 
#Create two integer queues
a=queue()
b=queue()
a.qput(10)
b.qput(19)
a.qput(20)
b.qput(1)

print a.qget(),
print a.qget(),
print b.qget(),
print b.qget()

#Create two double queues
c=queue()
d=queue()
c.qput(10.12)
d.qput(19.99)
c.qput(-20.0)
d.qput(0.986)

print c.qget(),
print c.qget(),
print d.qget(),
print d.qget()
10 20 19 1
10.12 -20.0 19.99 0.986

Example 16.8, Page Number: 387

In [8]:
class myclass:
    def __init__(self,a,b):
        self.__i=a
        self.__j=b
    def show(self):
        print self.__i,self.__j

ob1=myclass(10,0.23)
ob2=myclass('X',"This is a test")

#Result
ob1.show()   #Show int,double
ob2.show()   #Show char.char*
10 0.23
X This is a test

Example 16.9, Page Number: 388

In [1]:
 
def SIZE():
    return 10

class atype:
    def __init__(self):
        self.a=[]
        for i in range(SIZE()):
            self.a.append(i)
    #Implementing the [] overloading
    def op1(self,i,j):
        if (i<0 or i>=SIZE()):
            print "\nIndex value of ",
            print i," is out-of-bounds."
            return 
        self.a[i]=j
    def op2(self,i):
        if (i<0 or i>SIZE()-1):
            print "\nIndex value of ",
            print i," is out-of-bounds."
            return
        return self.a[i]
 
#Variable declaration
intob=atype()
doubleob=atype()
     
print "Integer array: ",
for i in range(SIZE()):
    intob.op1(i,i)
for i in range(SIZE()):
    print intob.op2(i),
    
print ""

print "Double array: ",
for i in range(SIZE()):
    doubleob.op1(i,float(i)/3)
for i in range(SIZE()):
    print doubleob.op2(i),
    
intob.op1(12,100)
Integer array:  0 1 2 3 4 5 6 7 8 9 
Double array:  0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 
Index value of  12  is out-of-bounds.

Example 16.10, Page Number: 389

In [2]:
class atype:
    def __init__(self,size):
        self.a=[]
        self.size=size
        for i in range(size):
            self.a.append(i)
    #Implementing the [] overloading
    def op1(self,i,j):
        if (i<0 or i>self.size-1):
            print "\nIndex value of ",
            print i," is out-of-bounds."
            return
        self.a[i]=j
    def op2(self,i):
        if (i<0 or i>self.size-1):
            print "\nIndex value of ",
            print i," is out-of-bounds."
            return
        return self.a[i]
 
#Variable declaration
intob=atype(10)
doubleob=atype(15)
     
print "Integer array: ",
for i in range(10):
    intob.op1(i,i)
for i in range(10):
    print intob.op2(i),
    
print ""

print "Double array: ",
for i in range(15):
    doubleob.op1(i,float(i)/3)
for i in range(15):
    print doubleob.op2(i),
    
intob.op1(12,100)                #generates runtime error
Integer array:  0 1 2 3 4 5 6 7 8 9 
Double array:  0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 3.33333333333 3.66666666667 4.0 4.33333333333 4.66666666667 
Index value of  12  is out-of-bounds.

Example 16.11, Page Number: 391

In [3]:
class atype:
    def __init__(self,size=10):
        self.a=[]
        for i in range(size):
            self.a.append(i)
    #Implementing the [] overloading
    def op1(self,i,j):
        if (i<0 and i>SIZE()-1):
            print "Index value of "
            print i," is out-of-bounds."
            exit(1)
        self.a[i]=j
    def op2(self,i):
        if (i<0 and i>SIZE()-1):
            print "Index value of "
            print i," is out-of-bounds."
            exit()
        return self.a[i]
 
#Variable declaration
intob=atype(100)
doubleob=atype()
defarray=atype()
     
print "Integer array: ",
for i in range(100):
    intob.op1(i,i)
for i in range(100):
    print intob.op2(i),
    
print ""

print "Double array: ",
for i in range(10):
    doubleob.op1(i,float(i)/3)
for i in range(10):
    print doubleob.op2(i),
    
print ""

print "Defarray array: ",
for i in range(10):
    doubleob.op1(i,i)
for i in range(10):
    print doubleob.op2(i),
Integer array:  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
Double array:  0.0 0.333333333333 0.666666666667 1.0 1.33333333333 1.66666666667 2.0 2.33333333333 2.66666666667 3.0 
Defarray array:  0 1 2 3 4 5 6 7 8 9

Example 16.12, Page Number: 393

In [4]:
class myclass:
    def __init__(self,a):
        if isinstance(a,int):
            print "Inside myclass<int>specialization"
            self.__x=a*a
        else:
            print "Inside generic myclass"
            self.__x=a
    def getx(self):
        return self.__x
        
d=myclass(10.1)
print "double: ",d.getx(),"\n"

i=myclass(5)
print "int: ",i.getx()
Inside generic myclass
double:  10.1 

Inside myclass<int>specialization
int:  25
In [ ]: