<< Chapter < Page Chapter >> Page >
Listing 4 . Object can modify itself at runtime.
class TestClass(object): def addInstanceVar(self,data):self.iVar = data

I am presenting and explaining this program for a couple of different reasons:

  • It will be useful in explaining the method named __init__ in the next program in this module.
  • It will be useful in explaining issues surrounding class variables in a future module.

Add an instance variable to one object and display it

The other difference between the two programs is shown in Listing 5 . Instead of accessing the object directly to add the new instance variable, as was done in Listing 2 , the code in Listing 5 calls the method named addInstanceVar on the object to cause the new instance variable to be added.

Listing 5 . Add an instance variable to one object and display it.
print("Add an instance variable to one object and display it") ref01.addInstanceVar(1234)print(ref01.iVar)

As mentioned earlier, the end result is the same as you will see if you examine the output from the program shown in Listing 11 .

Visualize the code in Listing 10

Once again, I recommend that you create a visualization for the code in Listing 10 and step through the program one instruction at a time. As you do that, pay attention tothe movements of the red and green arrows on the left, the diagram on the right, and the printed material at the bottom. That should help you to betterunderstand how a Python object can modify itself at runtime.

The method named __init__

You learned in the previous program that a method belonging to an object can add an instance variable to an object at runtime.

You also learned in an earlier module that if your class contains a method named __init__ , that method will be executed automatically in conjunction with the instantiation of the object. As I understand it, that isprobably the last thing that happens before the new object's reference is returned to be saved for later use.

A complete listing of a program that I will use to discuss the __init__ method is shown in Listing 11 . The output produced by that program is shown in Figure 7 . As usual, I will explain the program in fragments.

A class with an __init__ method

Listing 6 shows the definition of a class named TestClass that contains a method named __init__ in addition to a method named addInstanceVar as in the previous program.

Listing 6 . A class with an __init__ method.
class TestClass(object): def __init__(self,data):self.iVar01 = datadef addInstanceVar(self,data): self.iVar02 = data

When either of the methods shown in Listing 6 are executed, a new instance variable will be added to the object and initialized with an incoming datavalue. Since the method named __init__ is always executed automatically in conjunction with the instantiation of an object, the initial state of theobject will include an instance variable named iVar01 .

Instantiate and display two objects

The code in Listing 7 instantiates and prints two new objects of the class named TestClass . You can view the output produced by the code in Listing 7 in Figure 7 . There probably won't be any surprises there.

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