A trivial example:
#!/usr/bin/env python
class Animal(object):
pass
class Dog(Animal):
def talk(self):
return "whoof whoof"
class Cat(Animal):
def talk(self):
return "miao"
class Pig(Animal):
def talk(self):
return "oink oink"
def all_animals():
return Animal.__subclasses__()
if __name__ == "__main__":
for animal in all_animals():
print "%s says: %s" % (animal.__name__, animal().talk())
This way, if we add a new animal all_animals will work without any change from us.
4 comments:
What does "pass" mean?
http://docs.python.org/tutorial/controlflow.html#pass-statements
Yuck (the pass)
This was very helpful! Thanks :)
Post a Comment