Chapter 19: Run-Time Type ID and the Casting Operators

Example 19.1, Page Number: 453

In [2]:
class myclass:
    pass

#Variable declaration
i=j=0
f=0.0
ob=myclass()

print "The type of i is:",type(i).__name__
print "The type of f is:",type(f).__name__
print "The type of ob is:",ob.__class__.__name__
print "\n"

if type(i)==type(j):
    print "The types of i and j are the same"
    
if not(type(i)==type(f)):
    print "The types of i and f are not the same"
The type of i is: int
The type of f is: float
The type of ob is: myclass


The types of i and j are the same
The types of i and f are not the same

Example 19.2, Page Number: 454

In [3]:
class Base:
    pass
class Derived1(Base):
    pass
class Derived2(Base):
    pass

#Variable declaration
baseob=Base()
p=[Base()]
ob1=Derived1()
ob2=Derived2()


p[0]=baseob
print "p is pointing to an object of type",
print p[0].__class__.__name__

p[0]=ob1
print "p is pointing to an object of type",
print p[0].__class__.__name__

p[0]=ob2
print "p is pointing to an object of type",
print p[0].__class__.__name__
p is pointing to an object of type Base
p is pointing to an object of type Derived1
p is pointing to an object of type Derived2

Example 19.3, Page Number: 455

In [4]:
class Base:
    pass
class Derived1(Base):
    pass
class Derived2(Base):
    pass

def WhatType(ob):
    print "ob is referencing an object of type",
    print ob.__class__.__name__
    
    
#Variable declaration
baseob=Base()
p=[Base()]
ob1=Derived1()
ob2=Derived2()

WhatType(baseob)
WhatType(ob1)
WhatType(ob2)
ob is referencing an object of type Base
ob is referencing an object of type Derived1
ob is referencing an object of type Derived2

Example 19.4, Page Number: 456

In [5]:
import random

class figure:
    def __init__(self,i,j):
        self._x=i
        self._y=j
        
class triangle(figure):
    def __init__(self,i,j):
        figure.__init__(self,i,j)
    def area(self):
        return self._x*0.5*self._y
        
class rectangle(figure):
    def __init__(self,i,j):
        figure.__init__(self,i,j)
    def area(self):
        return self._x*self._y
        
class circle(figure):
    def __init__(self,i,j=0):
        figure.__init__(self,i,j)
    def area(self):
        return self._x*self._x*3.14
    
def factory():
    i=random.randint(0,2)
    if i==0:
        return circle(10.0)
    elif i==1:
        return triangle(10.1,5.3)
    elif i==2:
        return rectangle(4.3,5.7)
    

t=c=r=0   
p=[None]

#generate and count objects
for i in range(10):
    p[0]=factory()    #generate an object
    print "Object is ",p[0].__class__.__name__,". ",
    #count it
    if p[0].__class__.__name__==triangle.__name__:
        t+=1
    if p[0].__class__.__name__==rectangle.__name__:
        r+=1
    if p[0].__class__.__name__==circle.__name__:
        c+=1
    #display its area
    print "Area is",p[0].area()

print "Objects generated:"
print "Triangles:",t
print "Rectangles:",r
print "Circles:",c
Object is  circle .  Area is 314.0
Object is  rectangle .  Area is 24.51
Object is  rectangle .  Area is 24.51
Object is  circle .  Area is 314.0
Object is  rectangle .  Area is 24.51
Object is  circle .  Area is 314.0
Object is  rectangle .  Area is 24.51
Object is  triangle .  Area is 26.765
Object is  rectangle .  Area is 24.51
Object is  circle .  Area is 314.0
Objects generated:
Triangles: 1
Rectangles: 5
Circles: 4

Example 19.5, Page Number: 456

In [6]:
 
class myclass:
    def __init__(self,i):
        self.__a=i
        
o1=myclass(10)
o2=myclass(9)
o3=myclass(7.2)

print "Type of o1 is",o1.__class__.__name__

print "Type of o2 is",o2.__class__.__name__

print "Type of o3 is",o3.__class__.__name__

print

if o1.__class__.__name__==o2.__class__.__name__:
    print "o1 and o2 are the same type"
    
if o1.__class__.__name__==o3.__class__.__name__:
    print "Error"
else:
    print "o1 and o3 are different types"

#This prints error because python doesnt use templates.
Type of o1 is myclass
Type of o2 is myclass
Type of o3 is myclass

o1 and o2 are the same type
Error

Example 19.6, Page Number: 460

In [7]:
import random

class figure:
    def __init__(self,i,j):
        self._x=i
        self._y=j
        
class triangle(figure):
    def __init__(self,i,j):
        figure.__init__(self,i,j)
    def area(self):
        return self._x*0.5*self._y
        
class rectangle(figure):
    def __init__(self,i,j):
        figure.__init__(self,i,j)
    def area(self):
        return self._x*self._y
        
class circle(figure):
    def __init__(self,i,j=0):
        figure.__init__(self,i,j)
    def area(self):
        return self._x*self._x*3.14
    
def factory():
    i=random.randint(0,2)
    if i==0:
        return circle(10.0)
    elif i==1:
        return triangle(10.1,5.3)
    elif i==2:
        return rectangle(4.3,5.7)
    

t=c=r=0   
p=[None]

#generate and count objects
for i in range(10):
    p[0]=factory()    #generate an object
    print "Object is ",p[0].__class__.__name__,". ",
    #count it
    if p[0].__class__.__name__==triangle.__name__:
        t+=1
    if p[0].__class__.__name__==rectangle.__name__:
        r+=1
    if p[0].__class__.__name__==circle.__name__:
        c+=1
    #display its area
    print "Area is",p[0].area()

print "Objects generated:"
print "Triangles:",t
print "Rectangles:",r
print "Circles:",c
Object is  triangle .  Area is 26.765
Object is  circle .  Area is 314.0
Object is  circle .  Area is 314.0
Object is  rectangle .  Area is 24.51
Object is  rectangle .  Area is 24.51
Object is  rectangle .  Area is 24.51
Object is  circle .  Area is 314.0
Object is  circle .  Area is 314.0
Object is  rectangle .  Area is 24.51
Object is  triangle .  Area is 26.765
Objects generated:
Triangles: 2
Rectangles: 4
Circles: 4

Example 19.7, Page Number: 463

In [8]:
class Base:
    def f(self):
        print "Inside Base"
        
class Derived(Base):
    def f(self):
        print "Inside Derived"
        
bp=[Base()]       #pointer to base
b_ob=Base()    
dp=[Derived()]    #pointer to derived
d_ob=Derived()

dp[0]=d_ob
if dp[0]:
    print "Cast from Derived * to Derived * OK."
    dp[0].f()
else:
    print "Error"
print

bp[0]=d_ob
if bp[0]:
    print "Cast from Derived * to Base * OK."
    bp[0].f()
else:
    print "Error"
print

bp[0]=b_ob
if bp[0]:
    print "Cast from Base * to Base * OK."
    bp[0].f()
else:
    print "Error"
print

dp[0]=b_ob
if dp[0]:
    print "Error"
else:
    print "Cast from Base * to Derived * not OK."
print

bp[0]=d_ob      #bp points to Derived object
dp[0]=bp[0]
if dp[0]:
    print "Cast bp to a Derived * OK."
    print "because bp is really pointing\n",
    print "to a Derived object."
    dp[0].f()
else:
    print "Error"
print

bp[0]=b_ob      #bp points to Base object
dp[0]=bp[0]
if dp[0]:
    print "Error"
else:
    print "Now casting bp to a Derived *\n",
    print "is not OK because bp is really\n",
    print " pointing to a Base object."
print

dp[0]=d_ob      #dp points to Derived object
bp[0]=dp[0]
if bp[0]:
    print "Casting dp to a Base * is OK."
    bp[0].f()
else:
    print "Error"
print
Cast from Derived * to Derived * OK.
Inside Derived

Cast from Derived * to Base * OK.
Inside Derived

Cast from Base * to Base * OK.
Inside Base

Error

Cast bp to a Derived * OK.
because bp is really pointing
to a Derived object.
Inside Derived

Error

Casting dp to a Base * is OK.
Inside Derived

Example 19.8, Page Number: 465

In [9]:
class Base:
    def f(self):
        pass
        
class Derived(Base):
    def derivedOnly(self):
        print "Is a Derived Object"
        
bp=[Base()]       #pointer to base
b_ob=Base()    
dp=[Derived()]    #pointer to derived
d_ob=Derived()

#Use typeid

bp[0]=b_ob
if bp[0].__class__.__name__==Derived.__name__:
    dp[0]=bp[0]
    dp[0].derivedOnly()
else:
    print "Cast from Base to Derived failed."
    
bp[0]=d_ob
if bp[0].__class__.__name__==Derived.__name__:
    dp[0]=bp[0]
    dp[0].derivedOnly()
else:
    print "Error, cast should work!"
    
#Use dynamic_cast

bp[0]=b_ob
dp[0]=bp[0]
if dp[0].__class__.__name__==Derived.__name__:
    dp[0].derivedOnly()
else:
    print "Cast from Base to Derived failed."
    
bp[0]=d_ob
dp[0]=bp[0]
if dp:
    dp[0].derivedOnly()
else:
    print "Error, cast should work!"
    
Cast from Base to Derived failed.
Is a Derived Object
Cast from Base to Derived failed.
Is a Derived Object

Example 19.9, Page Number: 467

In [10]:
def f(p):
    v=p
    v[0]=100
    
#Variable declaration
x=[]

x.append(99)
print "x before call:",x[0]
f(x)
print "x after call:",x[0]
x before call: 99
x after call: 100

Example 19.10, Page Number: 468

In [11]:
f=199.22

i=f
print i
199.22

Example 19.11, Page Number: 469

In [12]:
i=0    #int
p="This is a string"

i=p     #cast pointer to integer   

#Result
print i
This is a string
In [ ]: