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

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
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