#!/usr/bin/env python """ Simple demo of object inheritence in python. Jim Mahoney, Nov 2006 The point is that a Cat object inherits from the Animal object, inheriting all its behavior. People also use the language that Cat is a 'child' of Animal, or that Animal is a 'parent' of Cat. thirty:Desktop$ ./inheritence_demo.py -- 1. Populate your zoo. -- Add an animal. The available animal types are (Cat, Dog, Mouse). Just hit 'return' if you don't want another. What type of animal? (e.g. 'Cat') Cat What's its name? (e.g. 'Fluffy') Fluffy Add an animal. The available animal types are (Cat, Dog, Mouse). Just hit 'return' if you don't want another. What type of animal? (e.g. 'Cat') cat What's its name? (e.g. 'Fluffy') Puss in Boots Add an animal. The available animal types are (Cat, Dog, Mouse). Just hit 'return' if you don't want another. What type of animal? (e.g. 'Cat') ajfkl;af *** Illegal input. Please try again. What type of animal? (e.g. 'Cat') dog What's its name? (e.g. 'Fluffy') Spot Add an animal. The available animal types are (Cat, Dog, Mouse). Just hit 'return' if you don't want another. What type of animal? (e.g. 'Cat') mouse What's its name? (e.g. 'Fluffy') Tiny Add an animal. The available animal types are (Cat, Dog, Mouse). Just hit 'return' if you don't want another. What type of animal? (e.g. 'Cat') -- 2. Here it is. -- The cat named 'Fluffy' says 'Meow'. The cat named 'Puss in Boots' says 'Meow'. The dog named 'Spot' says 'Bark'. The mouse named 'Tiny' says 'Squeak'. """ class Animal: """Animals have a species, name and a sound - that's about it.""" def __init__(self, species = "", name="", sound=""): self.name = name self.sound = sound self.species = species def __str__(self): return "The %s named '%s' says '%s'." \ % (self.species.lower(), self.name, self.sound) animal_types = ('Cat', 'Dog', 'Mouse') class Cat(Animal): def __init__(self, name="Fluffy"): Animal.__init__(self, species="Cat", sound="Meow", name=name) class Dog(Animal): def __init__(self, name="Fido"): Animal.__init__(self, species="Dog", sound="Bark", name=name) class Mouse(Animal): def __init__(self, name="Mickey"): Animal.__init__(self, species="Mouse", sound="Squeak", name=name) class Zoo: """A collection of animals.""" def __init__(self, animals=[]): self.animals = animals def add_animal(self, an_animal): self.animals.append(an_animal) def __str__(self): if not self.animals: return "No animals in the zoo." strings = [] for animal in self.animals: strings.append(str(animal)) return "\n".join(strings) def populate(self, input_output): """Add animals to the zoo.""" while True: animal = input_output.get_animal_from_user() if animal: self.add_animal(animal) else: return class InputOutput: """Get animals from user input and report results.""" def get_animal_from_user(self): """Return an animal chosen by the user, or False if none.""" print "Add an animal." print " The available animal types are (%s)." % ', '.join(animal_types) print " Just hit 'return' if you don't want another." while True: try: its_type = raw_input(" What type of animal? (e.g. 'Cat') ") if not its_type: return False # Convert e.g. 'cat' to 'Cat' its_type = its_type.lower().title() if not its_type in animal_types: raise Exception() its_name = raw_input(" What's its name? (e.g. 'Fluffy') ") # This next line could be written in a more awkward loop like this: # if its_type == 'Dog': return Dog(name=its_name) # elif its_type == 'Cat': return Cat(name=its_name): # elif its_type == 'Mouse': return Mouse(name=its_name): return eval("%s(name='%s')" % (its_type, its_name)) except Exception: print " *** Illegal input. Please try again." def main(self): """Main program even loop""" print "-- 1. Populate your zoo. --" zoo = Zoo() zoo.populate(self) print "-- 2. Here it is. --" print zoo InputOutput().main()