<< Chapter < Page Chapter >> Page >

Complete program listings

Complete listings of the programs discussed in this module, along with the outputs produced by those programs are provided below.

Listing 15 . Complete program listing.
# This program demonstrates the modification of a class after it has been # used to instantiate objects.#------------------------------------------------------------------------class TestClass(object): def addClassVar(self,data):TestClass.classVar02 = data print("Instantiate and display two objects of TestClass")ref01 = TestClass() print(ref01)ref02 = TestClass() print(ref02)print("Add a class variable directly.") TestClass.classVar01 = "ABCD"print("Add a class variable via an object.") ref01.addClassVar("1234")print("Display both class variables via one object") print("ref01: " + ref01.classVar01)print("ref01: " + ref01.classVar02) print("Display both class variables via the other object")print("ref02: " + ref02.classVar01) print("ref02: " + ref02.classVar02)
Figure 10 . Output from the code in Listing 15.
Instantiate and display two objects of TestClass<__main__.TestClass object at 0x02152930><__main__.TestClass object at 0x02152E70>Add a class variable directly. Add a class variable via an object.Display both class variables via one object ref01: ABCDref01: 1234 Display both class variables via the other objectref02: ABCD ref02: 1234
Listing 16 . Complete program listing.
# This program demonstrates that class variables are shared among the objects # instantiated from a class and that changes to class variables impact the# objects instantiated from the class. #----------------------------------------------------------------------------class TestClass(object): classVar = [1,2,3,4]def modifyClassVar01(self): self.classVar.append("a")def printClassVar01(self): print(str(self)[10:40]+ ": " + str(self.classVar)) print("1 Display class var using class: " + str(TestClass.classVar))print("2 Instantiate two objects")ref01 = TestClass() ref02 = TestClass()print("3 Display class var using objects") ref01.printClassVar01()ref02.printClassVar01() print("4 Modify class var using object")ref01.modifyClassVar01() print("5 Display class var using objects")ref01.printClassVar01() ref02.printClassVar01()print("6 Modify class var using class") TestClass.classVar.append(True)print("7 Display class var using objects") ref01.printClassVar01()ref02.printClassVar01() print("8 Display class var using class: " + str(TestClass.classVar))
Figure 11 . Output from the code in Listing 16.
1 Display class var using class: [1, 2, 3, 4] 2 Instantiate two objects3 Display class var using objects TestClass object at 0x02091DD0: [1, 2, 3, 4]TestClass object at 0x0218C710: [1, 2, 3, 4] 4 Modify class var using object5 Display class var using objects TestClass object at 0x02091DD0: [1, 2, 3, 4, 'a']TestClass object at 0x0218C710: [1, 2, 3, 4, 'a'] 6 Modify class var using class7 Display class var using objects TestClass object at 0x02091DD0: [1, 2, 3, 4, 'a', True]TestClass object at 0x0218C710: [1, 2, 3, 4, 'a', True] 8 Display class var using class: [1, 2, 3, 4, 'a', True]
Listing 17 . Complete program listing.
# This program illustrates that an instance variable will shadow or hide a # class variable with the same name.#--------------------------------------------------------------------------- class TestClass(object):classVar = 1234 #Note, the following code adds a new instance variable named classVar# to the object, which "shadows" or hides the actual class variable # named classVar insofar as this object is concerned.def shadowClassVariable(self): self.classVar = "ABCD"def printClassVar(self): print(str(self)[10:40]+ ": " + str(self.classVar)) print("1 Instantiate two objects")ref01 = TestClass() ref02 = TestClass()print("2 Display classVar using objects") ref01.printClassVar()ref02.printClassVar() print("3 Shadow the classVar in one object")ref01.shadowClassVariable() print("4 Display classVar using objects")ref01.printClassVar() ref02.printClassVar()print("5 Modify classVar using class") TestClass.classVar = Trueprint("6 Display classVar using objects") ref01.printClassVar()ref02.printClassVar()
Figure 12 . Output from the code in Listing 17.
1 Instantiate two objects 2 Display classVar using objectsTestClass object at 0x02201DD0: 1234 TestClass object at 0x022FC710: 12343 Shadow the classVar in one object 4 Display classVar using objectsTestClass object at 0x02201DD0: ABCD TestClass object at 0x022FC710: 12345 Modify classVar using class 6 Display classVar using objectsTestClass object at 0x02201DD0: ABCD TestClass object at 0x022FC710: True

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Itse1359-1440-Class Variables
  • File: Itse1359-1440.htm
  • Published: 10/27/14
  • Revised: 03/04/15
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

-end-

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