Chapter 12: A Closer Look at Classes

Example 12.1, Page Number: 274

In [1]:
class myclass:
    __a=None
    __b=None
    def __init__(self,i,j):
        self.__a=i
        self.__b=j
    def sum(self,x):   #Friend function
        return sum1(x)
        
def sum1(x):        
    return x._myclass__a +x._myclass__b   #accessing private members

#Variable declaration
n=myclass(3,4)

#Result
print n.sum(n)
7

Example 12.2, Page Number: 275

In [2]:
class c1:
    __status=None
    def set_status(self,state):
        self.__status=state
    
class c2:
    __status=None
    def set_status(self,state):
        self.status=state
    
#Friend function    
def idle(a,b):
    if a._c1__status or b._c2__status :
        return 0
    else:
        return 1
    
#variable declarations
def IDLE():             #Constants
    return 0
def INUSE():
    return 1
x=c1()
y=c2()

x.set_status(IDLE())
y.set_status(IDLE())

if idle(x,y):
    print "Screen Can Be Used."
    
x.set_status(INUSE())

if idle(x,y):
    print "Screen Can Be Used."
else:
    print "Pop-up In Use."
    
    
Screen Can Be Used.
Pop-up In Use.

Example 12.3, Page Number: 277

In [4]:
def IDLE():            
    return 0
def INUSE():
    return 1

class c1:
    __status=None
    def set_status(self,state):
        self.__status=state
    def idle(self,b):            #now a member of c1
        if self.__status or b._c2__status :
            return 0
        else:
            return 1
    
class c2:
    __status=None     #IDLE if off INUSE if on screen
    def set_status(self,state):
        self.status=state
        
#Variable declarations        
x=c1()
y=c2()

x.set_status(IDLE())
y.set_status(IDLE())

if idle(x,y):
    print "Screen Can Be Used."
    
x.set_status(INUSE())

if idle(x,y):
    print "Screen Can Be Used."
else:
    print "Pop-up In Use."
        
Screen Can Be Used.
Pop-up In Use.

Example 12.4, Page Number: 278

In [1]:
import time,string

class timer:
    __seconds=None
    
    def __init__(self,t1,t2=None):
        if t2==None:
            if isinstance(t1,int):   #seconds specified as an integer
                self.__seconds=t1
            else:                    #seconds specified as a string
                self.__seconds=string.atoi(t1)
        else:                        #time in minutes and seconds
            self.__seconds=t1*60+t2
            
    def run(self):
        t1=time.clock()
        while (time.clock()-t1)<self.__seconds:
            a=10
        print time.clock()-t1
 
        
a=timer(10)
b=timer("20")
c=timer(1,10)
a.run()            #count 10 seconds
b.run()            #count 20 seconds
c.run()            #count 1 minute,10 seconds
        
10.0000009774
20.0000009774
70.0000004887

Example 12.5, Page Number: 280

In [2]:
import time,string

class timer:
    __seconds=None
    
    def __init__(self,t1,t2=None):
        if t2==None:
            if isinstance(t1,int):   #seconds specified as an integer
                self.__seconds=t1
            else:                    #seconds specified as a string
                self.__seconds=string.atoi(t1)
        else:                        #time in minutes and seconds
            self.__seconds=t1*60+t2
            
    def run(self):
        t1=time.clock()
        while (time.clock()-t1)<self.__seconds:
            a=10
        print time.clock()-t1
            
a=timer(10)
a.run()

print "Enter number of seconds: "
str="20"
b=timer(str)                       #initialize at the run time
c.run()

print "Enter minutes and seconds: "
min=1
sec=10
c=timer(min,sec)                   #initialize at the run time
c.run()
        
10.0000004887
Enter number of seconds: 
70.0000009774
Enter minutes and seconds: 
70.0000009774

Example 12.6, Page Number: 282

In [1]:
class myclass:
    __a=None             #private members
    __b=None
    def setab(self,i,j): #publc functons
        self.__a=i
        self.__b=j
    def showab(self):
        print "a is ",self.__a
        print "b is ",self.__b

#Variable declaration
ob1 = myclass()
ob2 = myclass()

#Intalizing
ob1.setab(10,20)
ob2.setab(0,0)

print "ob1 before assignment: "
ob1.showab()
print "ob2 before assignment: "
ob2.showab()

ob2 = ob1            #assign ob1 to ob2

#Result
print "ob1 after assignment: "
ob1.showab()
print "ob2 after assignment: "
ob2.showab()
ob1 before assignment: 
a is  10
b is  20
ob2 before assignment: 
a is  0
b is  0
ob1 after assignment: 
a is  10
b is  20
ob2 after assignment: 
a is  10
b is  20

Example 12.7, Page Number: 283

In [3]:
from copy import deepcopy
        
class OBJ:
    def set_i(self,x):
        self.__i=x
    def out_i(self):
        print self.__i,
    
def f(x):
    x=deepcopy(x)
    x.out_i()     #outputs 10
    x.set_i(100)  #this affects only local copy
    x.out_i()     #outputs 100
    
#Variable declaration
o=OBJ()
o.set_i(10)
f(o)               
o.out_i()         #still outputs 10, value of i unchanged
10 100 10

Example 12.8, Page Number: 284

In [2]:
class myclass:                       
    def __init__(self,i):
        self.__val=i
        print "Constructing"
    def __del__(self):
        print "Destructing"
    def getval(self):
        return self.__val
        
def display(ob):
    print ob.getval()

#Varable declaration
a=myclass(10)

display(a)
Constructing
Destructing
10

Example 12.9, Page Number: 286

In [4]:
from ctypes import *

class myclass:
    def __init__(self,i):
        print "Allocating p"
        self.p=pointer(c_int(i))
    def __del__(self):
        print "Freeing p"
    def getval(self):
        return self.p[0]
        
def display(ob):
    print ob.getval()

#Variable declaration
a=myclass(10)

display(a)
    
Allocating p
10

Example 12.10, Page Number: 287

In [5]:
 
from ctypes import *
class myclass:
    def __init__(self,i):
        print "Allocating p"
        self.p=pointer(c_int(i))
    def __del__(self):
        print "Freeing p"
    def getval(self):
        return self.p[0]
        
def display(ob):
    print ob[0].getval()

#Variable declaration
a=[]
a.append(myclass(10))

display(a)
    
Freeing p
Allocating p
10

Example 12.11, Page Number: 288

In [6]:
class sample:
    __s=None
    def show(self):
        print self.__s
    def set(self,str):
        self.__s=str

#Return an object of type sample
def input():
    str=sample()
    instr = "Hello"       #User input
    str.set(instr)
    return str

#Variable declaration
ob=sample()

#assign returned object to ob
ob=input()

#Result
ob.show()
Hello

Example 12.12, Page Number: 289

In [8]:
class sample:
    __s=None
    def __init__(self):
        self.__s=0
    def __del__(self):          
        print "Freeing p"
    def show(self):
        print self.__s
    def set(self,str):
        self.__s=str
        
#This function takes one object parameter
def input():
    str=sample()
    instr="Hello"    #User input
    str.set(instr)
    return str

#Variable declaration
ob=sample()

#assign returned object to ob
ob=input()

#Result
ob.show()
Freeing p
Freeing p
Hello

Example 12.13, Page Number: 292

In [1]:
class myclass:
    __p=None
    def __init__(self,i):
        if isinstance(i,int):
            print "Allocating p"
            self.__p=i
        else:
            print "Copy constructor called"
            self.__p=i.getval()
    def __del__(self):          
        print "Freeing p"
    def getval(self):
        return self.__p
        
#This function takes one object parameter
def display(ob):
    print ob.getval()

#Variable declaration
ob=myclass(10)

#Result
display(ob)
Allocating p
10

Example 12.14, Page Number: 294

In [4]:
class myclass:
    __p=None
    def __init__(self,i):
        if isinstance(i,int):
            print "Allocating p"
            self.__p=i
        else:
            print "Copy constructor called"
            self.__p=i.getval()
    def __del__(self):          
        print "Freeing p"
    def getval(self):
        return self.__p
        

#Variable declaration
a=myclass(10)         #calls normal constructor
b=myclass(a)           #calls copy constructor
Allocating p
Copy constructor called

Example 12.15, Page Number: 295

In [8]:
class myclass:
    def __init__(self,i=0):
        if isinstance(i,int):
            print "Normal constructor"
        else:
            print "Copy constructor"


#Variable declaration
a=myclass()           #calls normal constructor

f=myclass()
a=myclass(f)          #Invoke copyconstructor
Normal constructor
Normal constructor
Copy constructor

Example 12.16, Page Number: 297

In [13]:
class c1:
    def __init__(self):
        self.__i=None
    def load_i(self,val):
        self.__i=val
    def get_i(self):
        return self.__i
    
#Variable declaration    
o=c1()

o.load_i(100)

#Result
print o.get_i()
100
In [ ]: