Chapter 7: Creating Classes and objects

Example 7.1, Page No.118

In [10]:
class dog:
    def bark(self):
        print "WOOF!"
    
    def setAge(self,age):
        self.age=age
    
    def setWeight(self,weight):
        self.weight=weight
    
    def setColor(self,color):
        self.color=color
    
    def getAge(self):
        return self.age
    
    def getWeight(self):
        return self.weight
    
    def getColor(self):
        return self.color
    
    
if __name__=='__main__':
    fido=dog()
    fido.setAge(3)
    fido.setWeight(15)
    fido.setColor("brown")
    
    print "Fido is a",fido.getColor()," dog"
    print "Fido is ",fido.getAge()," years old"
    print "Fido Weighs ",fido.getWeight()," pounds"
    fido.bark()
Fido is a brown  dog
Fido is  3  years old
Fido Weighs  15  pounds
WOOF!

Example 7.2, Page No.120

In [36]:
class dog:
    def bark(self):
        print "WOOF!"
    
    def setValues(self,age,weight,color):
        self.age=age
        self.weight=weight
        self.color=color
        
    def getAge(self):
        return self.age
    
    def getWeight(self):
        return self.weight
    
    def getColor(self):
        return self.color
    
    
if __name__=='__main__':
    fido=dog()
    fido.setValues(3,15,"brown")
    poonch=dog()
    poonch.setValues(4,18,"gray")
    print "Fido is a",fido.getColor()," dog"
    print "Fido is ",fido.getAge()," years old"
    print "Fido Weighs ",fido.getWeight()," pounds"
    fido.bark()
    
    print "Poonch is ",poonch.getAge()," years old ",poonch.getColor()," dog who weighs ",poonch.getWeight()," pounds",poonch.bark()
   
    
Fido is a brown  dog
Fido is  3  years old
Fido Weighs  15  pounds
WOOF!
Poonch is  4  years old  gray  dog who weighs  18  pounds WOOF!
None

Example 7.3, Page No.122

In [37]:
class dog:
    def __init__(self,age,weight,color):
        self.age=age
        self.weight=weight
        self.color=color
        
    def bark(self):
        print "WOOF!"
    
    def getAge(self):
        return self.age
    
    def getWeight(self):
        return self.weight
    
    def getColor(self):
        return self.color
    
    def __del__(self):
        print "Object destroyed"
    
if __name__=='__main__':
    fido=dog(3,15,"brown")
    poonch=dog(4,18,"gray")
   
    print "Fido is a",fido.getColor()," dog"
    print "Fido is ",fido.getAge()," years old"
    print "Fido Weighs ",fido.getWeight()," pounds"
    fido.bark()
    
    print "Poonch is ",poonch.getAge()," years old ",poonch.getColor()," dog who weighs ",poonch.getWeight()," pounds",poonch.bark()
   
    del fido
    del poonch
Fido is a brown  dog
Fido is  3  years old
Fido Weighs  15  pounds
WOOF!
Poonch is  4  years old  gray  dog who weighs  18  pounds WOOF!
None
Object destroyed
Object destroyed

Exmaple 7.4, Page No.124

In [1]:
class dog:
    def __init__(self,age=1,weight=2,color="black"):
        self.age=age
        self.weight=weight
        self.color=color 
    
    def bark(self,noise):
        print noise
    
    def getAge(self):
        return self.age
    
    def getWeight(self):
        return self.weight
    
    def getColor(self):
        return self.color
    
    def __del__(self):
        print "Object destroyed"
    
if __name__=='__main__':
    fido=dog(3,15,"brown")
    poonch=dog(4,18,"gray")
    rex=dog()
    sammy=dog(2,6,"white")
    
    
    print "Fido is a",fido.getColor()," dog"
    print "Fido is ",fido.getAge()," years old"
    print "Fido Weighs ",fido.getWeight()," pounds"
    fido.bark("WOOF!")
    
    print "Poonch is ",poonch.getAge()," years old ",poonch.getColor()," dog who weighs ",poonch.getWeight()," pounds",poonch.bark("WOOF!")
    print "Rex is ",rex.getAge()," years old ",rex.getColor()," dog who weighs ",rex.getWeight()," pounds",rex.bark("GRRR!")
    print "Sammy is ",sammy.getAge()," years old ",sammy.getColor()," dog who weighs ",sammy.getWeight()," pounds",sammy.bark("BOWOW!")
    del fido
    del poonch
    del rex
    del sammy
Fido is a brown  dog
Fido is  3  years old
Fido Weighs  15  pounds
WOOF!
Poonch is  4  years old  gray  dog who weighs  18  pounds WOOF!
None 
Rex is  1  years old  black  dog who weighs  2  pounds GRRR!
None 
Sammy is  2  years old  white  dog who weighs  6  pounds BOWOW!
None 
Object destroyed
Object destroyed
Object destroyed
Object destroyed

Example 7.5, Page No.126

In [6]:
class Polygon:
    weight=0
    height=0
    def setValues(self,w,h):
        self.weight=w
        self.height=h

class Rectangle(Polygon):
    def area(self):
        return self.weight*self.height

class Triangle(Polygon):
    def area(self):
        return (self.weight*self.height)/2

rect=Rectangle()
trgl=Triangle()
rect.setValues(4,5)
trgl.setValues(4,5)
print "Rectangle Area:",rect.area()
print "Triangle Area:",trgl.area()
Rectangle Area: 20
Triangle Area: 10

Example 7.6, Page No.128

In [10]:
class Parent:
    def __init__(self):
        print "Default Parent Constructor Called"
    def __init__(self,a):
        print "Overloaded Parent Constructor Called"

class Daughter(Parent):
    def __init__(self):
        print "Derived Daughter class default constructor called"

class Son(Parent):
    def __init__(self,a):
        Parent.__init__(self,a)
        print "Derived Son class overloaded constructor called"

emma=Daughter()
andrew=Son(0)
Derived Daughter class default constructor called
Overloaded Parent Constructor Called
Derived Son class overloaded constructor called

Example No.7.7, Page No. 130

In [22]:
class Man:
    def Speak(self, msg="Hello"):
        print "   ",msg

class Hombre(Man):
    def Speak(self,msg):
        print msg
henry=Man()
enrique=Hombre()
henry.Speak()
henry.Speak("It's a beautiful evening.")
enrique.Speak("Hola!")
enrique.Speak("Es una trade hermosa")
    Hello
    It's a beautiful evening.
Hola!
Es una trade hermosa