Another important aspect of Python classes is scope. The scope of a variable is the context in which it’s visible to the program.
It may surprise you to learn that not all variables are accessible to all parts of a Python program at all times. When dealing with classes, you can have variables that are available everywhere (global variables), variables that are only available to members of a certain class (member variables), and variables that are only available to particular instances of a class (instance variables).
The same goes for functions: some are available everywhere, some are only available to members of a certain class, and still others are only available to particular instance objects.
Check out the code in the editor. Note that each individual animal gets its own
age (since they’re all initialized individually), but they all have access to the member variable
is_alive, since they’re all members of the
Animal class. Click Run to see the output!