Chapter 11: Introducing the Class

Example 11.1, Page Number: 249

In [3]:
class queue:
    #Initialize the queue 
    def __init__(self):
        self.__sloc=0
        self.__rloc=-1
        self.__q=[]
        
    #Put an integer into the queue
    def qput(self,i):
        if self.__sloc==100:
            print "Queue is full."
            return
        self.__sloc+=1
        self.__q.append(i)
        
    #Get an integer from the queue
    def qget(self):
        if self.__rloc==self.__sloc:
            print "Queue underflow"
            return
        self.__rloc+=1
        return self.__q[self.__rloc]     
    

    
#Create two queue objects
b=queue()
a=queue()

a.qput(10)
b.qput(19)

a.qput(20)
b.qput(1)

#Result
print "Contents of queue a: ",
print a.qget(),' ',a.qget()

print "Contents of queue b: ",
print b.qget(),' ',b.qget()


    
Contents of queue a:  10   20
Contents of queue b:  19   1

Example 11.2, Page Number: 250

In [4]:
    
class myclass:
    def __init__(self):
        self.__a=None  #private member
        self.b=None    #public member
    #public functions
    def setlab(self,i):
        self.__a=i      #refer to a
        self.b=i*i    #refer to b
        return
    def geta(self):
        return self.__a #refer to a
    def reset(self):
        #call setlab using self
        self.setlab(0)     #the object is already known
            
        
ob=myclass()
ob.setlab(5)      #set ob.a and ob.b
print "ob after setlab(5): ",ob.geta(),' ',
print ob.b #can access b because it is public

ob.b=20    #can access b because it is public
print "ob after ob.b=20: ",
print ob.geta(),' ',ob.b

ob.reset()
print "ob after ob.reset(): ",ob.geta(),' ',ob.b
ob after setlab(5):  5   25
ob after ob.b=20:  5   20
ob after ob.reset():  0   0

Example 11.3, Page Number: 254

In [2]:
class queue:
    
    #Constructor
    def __init__(self):      
        self.__q=[]
        self.__rloc=-1
        self.__sloc=0
        print "Queue initialized"
        
    #Destructor
    def __del__(self):
        print ("Queue destroyed")
        
    #Put an integer into the queue
    def qput(self,i):
        if self.__sloc == 100:
            print "Queue is full"
            return 
        self.__sloc+=1
        self.__q.append(i)
        
    #Get an integer from the queue
    def qget(self):
        if self.__rloc==self.__sloc:
            print "Queue underflow"
            return
        self.__rloc+=1
        return self.__q[self.__rloc]
        
#Create two queue objects
a=queue()


a.qput(10)
a.qput(20)
b=queue()
b.qput(19)

b.qput(1)

#Result
print a.qget(),' ',
print a.qget(),' ',
print b.qget(),' ',
print b.qget(),' '
Queue initialized
Queue destroyed
Queue initialized
Queue destroyed
10   20   19   1  

Example 11.4, Page Number: 257

In [3]:
class queue:                            
    
    #Constructor
    def __init__(self,i):      
        self.__q=[]
        self.__rloc=-1
        self.__sloc=0
        self.__who=i
        print "Queue ",self.__who," initialized."
        
    #Destructor
    def __del__():
        print "Queue ",self.__who," destroyed"
        
    #Put an integer into the queue
    def qput(self,i):
        if self.__sloc == 100:
            print "Queue is full"
            return 
        self.__sloc+=1
        self.__q.append(i)
        
    #Get an integer from the queue
    def qget(self):
        if self.__rloc==self.__sloc:
            print "Queue underflow"
            return
        self.__rloc+=1
        return self.__q[self.__rloc]
        
a=queue(1)
b=queue(2)

a.qput(10)
b.qput(19)

a.qput(20)
b.qput(1)

#Result
print a.qget(),' ',
print a.qget(),' ',
print b.qget(),' ',
print b.qget(),' '
Queue  1  initialized.
Queue destroyed
Queue  2  initialized.
Queue destroyed
10   20   19   1  

Example 11.5, Page Number: 258

In [5]:
class widget:
    
    #Pass two arguments to the constructor
    def __init__(self,a,b):
        self.__i=a
        self.__j=b
        
    def put_widget(self):
        print self.__i," ",self.__j

#Initializing
x=widget(10,20)
y=widget(0,0)

x.put_widget()
y.put_widget()

    
10   20
0   0

Example 11.6, Page Number: 259

In [1]:
class myclass:
    
    #Constructor
    def __init__(self,x):
        self.a=x
    #To get the vale of a
    def get_a(self):
        return self.a

#Initializing
ob=myclass(4)

#Result
print ob.get_a()
4

Example 11.7, Page Number: 260

In [1]:
from ctypes import *

class c1(Structure):
    _fields_=[("__i", c_int)]  #private member
    def get_i(self):           #public finctions
        return self.__i
    def put_i(self,j):
        self.__i=j

#Variable declaration
s=c1()

s.put_i(10)

#Result
print s.get_i()
        
    
10

Example 11.8, Page Number: 261

In [2]:
class c1:
    def __init__(self):
        self.__i=None   #private member
    def get_i(self):    #public finctions
        return self.__i
    def put_i(self,j):
        self.__i=j

#Variable declaration
s=c1()

s.put_i(10)

#Result
print s.get_i()
        
    
10

Example 11.9, Page Number: 263

In [2]:
from ctypes import *

#Creates a union
class u_type(Union):
    _fields_ = [("i",c_short),
            ("ch", c_char*2)]
    #Constructor
    def __init__(self,a):
        self.i=a
        
    #Show the characters that comprise a short int.
    def showchars(self):
        print self.ch[0]
        print self.ch[1]
        
        
u=u_type(1000)

#Displays char of 1000
u.showchars()
�


Example 11.10, Page Number: 264

In [2]:
class c1:
    def __init__(self):
        self.__i=None
    def get_i(self):
        return self.i
    def put_i(self,j):
        self.i=j

#Variable declaration
s=c1()

s.put_i(10)

#Result
print s.get_i()
10

Example 11.11, Page Number: 265

In [3]:
class c1:
    def __init__(self):
        self.__i=None
    def get_i(self):
        return self.i
    def put_i(self,j):
        self.i=j

#Variable declaration
s=c1()

s.put_i(10)

#Result
print s.get_i()
10

Example 11.12, Page Number: 267

In [3]:
class display:
    def __init__(self):
        width=None
        height=None
        res=None
    def set_dim(self,w,h):
        self.width=w
        self.height=h
    def get_dim(self):
        return self.width,self.height
    def set_res(self,r):
        self.res=r
    def get_res(self):
        return self.res
        
#Variable decleration
names=["low","medium","high"]   
(low,medium,high)=(0,1,2)    #For enumeration type
w=None
h=None
display_mode=[]*3

for i in range(3):
    display_mode.append(display())

#Initialize the array of objects using member functions
display_mode[0].set_res(low)
display_mode[0].set_dim(640,480)

display_mode[1].set_res(medium)
display_mode[1].set_dim(800,600)

display_mode[2].set_res(high)
display_mode[2].set_dim(1600,1200)

#Result
print "Available display modes: "
for i in range(3):
    print names[display_mode[i].get_res()]," : ",
    w,h=display_mode[i].get_dim()
    print w," by ",h
Available display modes: 
low  :  640  by  480
medium  :  800  by  600
high  :  1600  by  1200

Example 11.13, Page Number: 268

In [4]:
class samp:
    __a=None
    def __init__(self,n):
        self.__a=n
    def get_a(self):
        return self.__a

#Initializing the list
sampArray=[samp(-1),samp(-2),samp(-3),samp(-4)]

#Display
for i in range(4):
    print sampArray[i].get_a(),' ',
-1   -2   -3   -4  

Example 11.14, Page Number: 269

In [5]:
class samp:
    __a=None
    __b=None
    def __init__(self,n,m):
        self.__a=n
        self.__b=m
    def get_a(self):
        return self.__a
    def get_b(self):
        return self.__b
 
#Initializing the list
sampArray=[[samp(1,2),samp(3,4)],
           [samp(5,6),samp(7,8)],
           [samp(9,10),samp(11,12)],
           [samp(13,14),samp(15,16)]]

#Display
for i in range(4):
    print sampArray[i][0].get_a(),' ',
    print sampArray[i][0].get_b()
    print sampArray[i][1].get_a(),' ',
    print sampArray[i][1].get_b()
1   2
3   4
5   6
7   8
9   10
11   12
13   14
15   16

Example 11.15, Page Number: 270

In [6]:
from ctypes import *

class P_example(Structure):
    __num=None
    def set_num(self,val):
        self.__num=val
    def show_num(self):
        print self.__num

#Variable declaration
ob=P_example()          #Declare an object to the structure
p=POINTER(P_example)    #Declare a pointer to the structure

ob.set_num(1)           #access ob directly
ob.show_num()

p=ob                    #assign p the address of ob
p.show_num()            #access ob using pointer
        
        
1
1

Example 11.16, Page Number: 271

In [7]:
class P_example(Structure):
    __num=None
    def set_num(self,val):
        self.__num=val
    def show_num(self):
        print self.__num
        
#Variable declaration
ob=[P_example(),P_example()]          #Declare an object to the structure
p=POINTER(P_example)                  #Declare a pointer to the structure

ob[0].set_num(10)                     #access objects directly
ob[1].set_num(20)

p=ob                             #obtain pointer to first element
p[0].show_num()                  #access ob using pointer

p[1].show_num()

p[0].show_num()
10
20
10
In [ ]: