<< 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 mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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