<< Chapter < Page Chapter >> Page >
Figure 5 . Output from code in Listing 9.
Instantiate and display two objects of TestClass<__main__.TestClass object at 0x00274910><__main__.TestClass object at 0x005A25F0>Add an instance variable to one object and display it 1234Try to display the same instance variable in the other object Traceback (most recent call last):File "1359-1430-13.py", line 18, in<module>print(ref02.iVar) AttributeError: 'TestClass' object has no attribute 'iVar'
Listing 10 . Complete program listing.
# Illustrates ability for object to modify itself at runtime #---------------------------------------------------------------------------class TestClass(object): def addInstanceVar(self,data):self.iVar = data print("Instantiate and display two objects of TestClass")ref01 = TestClass() print(ref01)ref02 = TestClass() print(ref02)print("Add an instance variable to one object and display it") ref01.addInstanceVar(1234)print(ref01.iVar) print("Try to display the same instance variable in the other object")print(ref02.iVar)
Figure 6 . Output from code in Listing 10.
Instantiate and display two objects of TestClass<__main__.TestClass object at 0x00224910><__main__.TestClass object at 0x00512850>Add an instance variable to one object and display it 1234Try to display the same instance variable in the other object Traceback (most recent call last):File "1359-1430-15.py", line 20, in<module>print(ref02.iVar) AttributeError: 'TestClass' object has no attribute 'iVar'
Listing 11 . Complete program listing.
# This program illustrates instance variables and the __init__ method #---------------------------------------------------------------------------class TestClass(object): def __init__(self,data):self.iVar01 = datadef addInstanceVar(self,data): self.iVar02 = dataprint("Instantiate and display two objects of TestClass") ref01 = TestClass("ABCD")print(ref01) ref02 = TestClass("DEFG")print(ref02) print("Display first instance variable from both objects")print(ref01.iVar01) print(ref02.iVar01)print("Add an instance variable to one object and display it") ref01.addInstanceVar(1234)print(ref01.iVar02) print("Try to display the same instance variable in the other object")print(ref02.iVar02)
Figure 7 . Output from code in Listing 11.
Instantiate and display two objects of TestClass<__main__.TestClass object at 0x00492E50><__main__.TestClass object at 0x00492E70>Display first instance variable from both objects ABCDDEFG Add an instance variable to one object and display it1234 Try to display the same instance variable in the other objectTraceback (most recent call last): File "1359-1430-17.py", line 26, in<module>print(ref02.iVar02) AttributeError: 'TestClass' object has no attribute 'iVar02'

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Itse1359-1430-Instance Variables
  • File: Itse1359-1430.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