<< 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.
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> |
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.
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.
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) .
Notification Switch
Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?