Page 348 - Beginning Programming with Pyth - John Paul Mueller
P. 348

message provides a specific greeting for each of the instances. In addition, each instance is separate, and you were able to change the message for the first instance without affecting the second instance.
FIGURE 15-7: The first version of the constructor provides a default value for the name.
FIGURE 15-8: Supplying the constructor with a name provides a customized output.
Working with variables
As mentioned earlier in the book, variables are storage containers that hold data. When working with classes, you need to consider how the data is stored and managed. A class can include both class variables and instance variables. The class variables are defined as part of the class itself, while instance variables are defined as part of methods. The following sections show how to use both variable types.
Creating class variables
Class variables provide global access to data that your class manipulates in some way. In most cases, you initialize global variables by using the constructor to ensure that they contain a known good value. The following steps demonstrate how class variables work.
1. Type the following code (pressing Enter after each line and pressing Enter twice after the last line):
    


























































































   346   347   348   349   350