Overriding the Constructor : Constructor : Class PYTHON TUTORIALS


PYTHON TUTORIALS » Class » Constructor »

 

Overriding the Constructor


class Bird:
   def __init__(self):
      self.hungry = 1

   def eat(self):
     if self.hungry:
        print 'Aaaah...'
        self.hungry = 0
     else:
        print 'No, thanks!

b = Bird() 
b.eat() 
b.eat() 


class Bird:
   def __init__(self):
      self.hungry = 1

   def eat(self):
     if self.hungry:
        print 'Aaaah...'
        self.hungry = 0
     else:
        print 'No, thanks!


class SongBird(Bird)
    def __init__(self)
        Bird.__init__(self)
        self.sound = 'Squawk!
    
    def sing(self)
        print self.sound 

sb = SongBird() 
sb.sing() 
sb.eat()



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Class
» Constructor