<< 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 we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
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
How can I make nanorobot?
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
how can I make nanorobot?
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
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?