# Garfield High School, Helene Martin # Demonstrates creating of a simple cat class # By convention, class names start with a capital letter class Cat: # Set default values for state at top lives = 9 hungry = True # The __init__ method is called a constructor # It is called whenever an object of type Cat is created # The self parameter is always necessary when writing methods def __init__(self, hungry): # set the object's hunger to a value passed in on creation self.hungry = hungry # The __str__ method is called when objects are printed def __str__(self): cat = "" cat += " /\\_/\\\n" # Note that the \ character must be escaped! cat += " ( o.o )\n" cat += " > ^ < \n" cat += "I am a cat of " + str(self.lives) + " lives" return cat def meow(self): print "meow! PURRRRR..." def eat(self): if(self.hungry): self.meow() self.hungry = False else: print "Not hungry!"