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

Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
Difference between extinct and extici spicies
Amanpreet Reply
While the American heart association suggests that meditation might be used in conjunction with more traditional treatments as a way to manage hypertension
Beverly Reply
Researchers demonstrated that the hippocampus functions in memory processing by creating lesions in the hippocampi of rats, which resulted in ________.
Mapo Reply
The formulation of new memories is sometimes called ________, and the process of bringing up old memories is called ________.
Mapo Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?