<< Chapter < Page Chapter >> Page >
Listing 1 . A simple class definition.
class TestClass(object): def addClassVar(self,data):TestClass.classVar02 = data

An object instantiated from the class in Listing 1 will not contain any data -- no class variables and no instance variables.

Instantiate and display two objects

The code in Listing 2 instantiates and prints two objects of the class named TestClass . Neither object contains any data and neither class hasaccess to any data.

Listing 2 . Instantiate and display two objects.
print("Instantiate and display two objects of TestClass") ref01 = TestClass()print(ref01) ref02 = TestClass()print(ref02)

Figure 1 shows the output from the code in Listing 2 . There is nothing new here. You saw code like this in an earlier module.

Figure 1 . Output from the code in Listing 2.
Instantiate and display two objects of TestClass<__main__.TestClass object at 0x02152930><__main__.TestClass object at 0x02152E70>

Add a class variable directly

The code in Listing 3 accesses the class named TestClass directly by name and adds a class variable named classVar01 to the class.

Listing 3 . Add a class variable directly.
print("Add a class variable directly.") TestClass.classVar01 = "ABCD"

The interesting question is what impact if any does this have on the two objects that have already been instantiated from the class? I will defer adiscussion of this question until after we see the impact.

Add a class variable via an object

The code in Listing 4 calls the method named addClassVariable that is defined in the class shown in Listing 1 to add another class variable named classVar02 to the class named TestClass .

Listing 4 . Add a class variable via an object.
print("Add a class variable via an object.") ref01.addClassVar("1234")

The purpose of including this code in the program is simply to show that an object can be used to modify the class from which it was instantiated.

Display both class variables via one object

The code in Listing 5 uses the reference to one of the objects, ref01 , to access and print the values of the two new class variables named classVar01 and classVar02 . That brings us back to the question of what impact if any does the addition of two new class variables haveon the two objects that have already been instantiated from the class?

Listing 5 . Display both class variables via one object.
print("Display both class variables via one object") print("ref01: " + ref01.classVar01)print("ref01: " + ref01.classVar02)

Although I am unable to locate a reference to verify this, it is my understanding that when program codereferences an attribute such as classVar01 using a reference to an object such as ref01 , the runtime system first searches the object to see if it contains anattribute with that name. If it doesn't find an attribute with that name in the object, it then searches the class from which the object wasinstantiated.

The object referred to by ref01 does not contain an attribute named classVar01 , but the class named TestClass does now contain an attribute with that name (even though it was not there when the object was instantiated) .

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