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

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
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