[Tutor] Problems with references

Lloyd Kvam pythonTutor at venix.com
Tue Jun 1 17:29:49 EDT 2004

I assume that you made the dictionary a class variable.  Class variables
are common to the class.  When they are immutable such as strings or
numbers, the CHANGED values will be per instance (when assigned to
self).  This is a handy way to provide default values within a class.

(I simply typed this in.  There could be minor typos)
class A:
	foo = 3

	def change_foo(self):
		print self.foo	# 3
		self.foo = 10
		print self.foo	# 10

print A.foo	# 3 unless class variable is explicitly changed

Since the dictionary is mutable, all changes are shared among
instances.  To have separate dictionaries for each instance move the
dictionary creation to the __init__ method and delete the class variable
from the source.

If the dictionary is NOT a class variable, please let us know.  Then it
will take further investigation.

On Tue, 2004-06-01 at 16:48, Martin Hjort Eriksen wrote:
> Hello
> This is a question on how references work with Python.
> I have several objects, which use the same class definition. Within this 
> class definition, there is an attribute of the type dictionary. During 
> the run of the application, this dictionary, will be slowly filled up 
> with references to other objects.
> I would expect that the dictionary attribute will not be the same 
> accross the objects. But everytime i add an object to one dictionary, it 
> is also added to all the other dictionary.
> When I compare the addresses in the memory for the objects, I can only 
> conclude that they are not the same.
> Here is an sample of when i put the references into the dictionaries:
>     def examineParticipations(self):
>         for id, participation in self.Participations.iteritems():
>             # lets first figure out what type it is
>             connection1ObjectType = 
> self.findDiaObject(participation.connection1)
>             connection2ObjectType = 
> self.findDiaObject(participation.connection2)
>             # attr =======================================================
>             if connection1ObjectType == "attribute":
>                 connection1Object = 
> self.Attributes[participation.connection1]
>                 # attr 
> -----------------------------------------------------------------------------------------------
>                 if connection2ObjectType == "entity":
>                     connection2Object = 
> self.Entities[participation.connection2]
> connection2Object.attributes[participation.connection1] = connection1Object
> Now for the question:
> Why is a references added into one dictionary attribute also added to 
> all other dictionary attributes, in objects of teh the same class 
> definition, when one would expect that they where different dictionaries?
> regards
> Martin Hjort Eriksen
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582

More information about the Tutor mailing list