<< Chapter < Page | Chapter >> Page > |
Figure 4 shows the output produced by the code in Listing 5 .
Figure 4 . Output from the code in Listing 5. |
---|
Available stations in Dallas
{98.3: 'KNON', 98.7: 'KLUV', 91.7: 'KKXT', 97.9: 'KBFB'}Program the buttons
Play the three programmed stationsPlaying KNON
Playing KBFBPlaying KKXT |
The definition of the class named Radio begins in Listing 6 . The first line of code in a class definition consists of the keyword classfollowed by the name of the class. This is followed by the name of another class in parentheses, which I will explain in a future module on class inheritance . As is common in Python, the closing parenthesis is followed by a colon (:)character. The colon is followed by the indented class body.
Listing 6 . Beginning of the class named Radio. |
---|
class Radio(object):
#This class provides the plans from which the radio objects are built.stations = {"Austin":{91.7:"KVRX",95.5:"KKMJ",98.1:"KVET",93.7:"KLBJ"},
"Dallas":{98.3:"KNON",91.7:"KKXT",97.9:"KBFB",98.7:"KLUV"}} |
The class body in Listing 6 begins with a variable named stations that references a dictionary object. The dictionary object contains twonested dictionary objects. One of the nested objects provides information about radio stations in Austin. The other nested object provides informationabout radio stations in Dallas. This is the source of the radio call sign information for Austin and Dallas shown in Figure 3 and Figure 4 .
IMPORTANT: According to The Python Tutorial -- Class and Instance Variables , the variable named stations is a class variable that is shared by all instances (objects) of the class. I will have more to say about class variables and instance variables in a future module.
As I mentioned earlier, when a function is defined inside of a class definition, it is called a method. Listing 7 defines a special method named __init__ . (Note that two underscore characters are required on each side of the word init .)
Listing 7 . The __init__ method. |
---|
def __init__(self):
self.stationNumber = [0,0,0] |
According to The Python Tutorial -- Class Objects ,
"When a class defines an __init__() method, class instantiation automatically invokes __init__() for the newly-created class instance."
Thus, the __init__ method can be used to initialize various aspects of a new object when it is instantiated. (This is roughly analogous to the constructor in other OO languages.)
The code inside the __init__ method in Listing 7 creates a new variable named stationNumber and initializes it to refer to a three-element list.
According to The Python Tutorial -- Class and Instance Variables , variables that are created in this manner (inside class methods) are called instance variables and are "for data unique to each instance (object)."
Therefore, the variable named stations from Listing 6 is shared among radio01 and radio02 in Listing 9 . However, radio01 and radio02 each has its own copy of the variable named stationNumber from Listing 7 and those variables are not shared among objects.
Notification Switch
Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?