Chapter Six : Conversions

EXAMPLE 6.1 page no : 48

In [1]:
large = 456789; # Potentially dangerous conversion
size = float(large)
print size
456789.0

EXAMPLE 6.2 page no : 49

In [2]:
class DangerousString :
    def __init__(self):
        pass # self.s = ''

    def __str__(self):
        pass # return self.s

EXAMPLE 6.3 page no : 51

In [9]:
large = 456789
size = int(large);
three = "three" 
print large,size,three
456789 456789 three

EXAMPLE 6.4 page no : 51

In [5]:
class B:
    pass
    
class D( B):
    pass

class E:
    pass
    
def dynamicCast(b):
     
    return D(b)

def constCast(d1):
    
    return D(d1)

def reinterpretCast(d):
    
    return E(d)

EXAMPLE 6.5 page no : 52

In [6]:
def addToFileList(s):
    pass
    
def addFiles(s):
    m = len(s)
    for i in range(m):
        addToFileList(s[i])

EXAMPLE 6.6 page no : 52

In [7]:
 
ci = 22;
pi = ci # NO: Const cast away
i = pi; # OK
 
pi = 7 
print ci, pi, i 
22 7 22

example 6.7 page no : 53

In [8]:
class EmcMatrix:
    def __init__(self):
        self.isDirtyM = False
        self.detM = 0.0

    def calculateDeterminant(self):
        pass

    def determinant(self):
        if(isDirtyM):
            # OK, access to mutable data members
            self.detM = self.calculateDeterminant();
            self.isDirtyM = False;
        return self.detM;
In [ ]: