<< 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 class named Radio

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"}}

A class variable named stations

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.

The __init__ method

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.

Questions & Answers

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?

Ask