Learn
Introduction to Classes
Class Scope

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.

Instructions

1.

Check out the code in the editor. Note that each individual animal gets its own name and 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!

Folder Icon

Take this course for free

Already have an account?