<< 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 are types of cell
Nansoh Reply
how can I get this book
Gatyin Reply
what is lump
Chineye Reply
what is cell
Maluak Reply
what is biology
Maluak
what's cornea?
Majak Reply
what are cell
Achol
Explain the following terms . (1) Abiotic factors in an ecosystem
Nomai Reply
Abiotic factors are non living components of ecosystem.These include physical and chemical elements like temperature,light,water,soil,air quality and oxygen etc
Qasim
what is biology
daniel Reply
what is diffusion
Emmanuel Reply
passive process of transport of low-molecular weight material according to its concentration gradient
AI-Robot
what is production?
Catherine
Pathogens and diseases
how did the oxygen help a human being
Achol Reply
how did the nutrition help the plants
Achol Reply
Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
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
cell is the smallest unit of the humanity biologically
Abraham
ok
Achol
what is biology
Victoria Reply
what is biology
Abraham
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