[Tutor] Problems with references

Martin Hjort Eriksen martin at hardcoder.dk
Tue Jun 1 16:48:06 EDT 2004


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



More information about the Tutor mailing list