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
- In the example above, we create two instances of an
Animal
. - Then we print out
True
, the default value stored in hippo’sis_alive
member variable. - Next, we set that to False and print it out to make sure.
- Finally, we print out
True
, the value stored in cat’sis_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
.