Skip to Content
Learn
Introduction to Classes
They're Multiplying!

A class can have any number of member variables. These are variables that are available to all members of a class.

hippo = Animal("Jake", 12) cat = Animal("Boots", 3) print hippo.is_alive hippo.is_alive = False print hippo.is_alive print cat.is_alive
  1. In the example above, we create two instances of an Animal.
  2. Then we print out True, the default value stored in hippo’s is_alive member variable.
  3. Next, we set that to False and print it out to make sure.
  4. Finally, we print out True, the value stored in cat’s is_alive member variable. We only changed the variable in hippo, not in cat.

Let’s add another member variable to Animal.

Instructions

1.

After line 3, add a second member variable called health that contains the string "good".

Then, create two new Animals: sloth and ocelot. (Give them whatever names and ages you like.)

Finally, on three separate lines, print out the health of your hippo, sloth, and ocelot.

Folder Icon

Take this course for free

Already have an account?