Advertisement
jseabold

Super Example

Aug 15th, 2011
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. class Animal(object):
  2.     def __init__(self, sound):
  3.         self.sound = sound
  4.  
  5.     def make_sound(self):
  6.         print "I am a", self.__class__.__name__
  7.         print "and I go", self.sound
  8.  
  9. class Dog(Animal):
  10.     def __init__(self, sound='bark'):
  11.         super(Dog, self).__init__(sound)
  12.  
  13. class Duck(Animal):
  14.     pass
  15.  
  16. # This is bad form, since Animal.make_sound is an instance method
  17. class Monster(Animal):
  18.     def make_sound(self):
  19.         Animal.make_sound(self)
  20.  
  21. dog = Dog()
  22. dog.make_sound()
  23.  
  24. duck = Duck('quack')
  25. duck.make_sound()
  26.  
  27. monster = Monster('ARGH')
  28. monster.make_sound()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement