Lists & "pointers"

Jan Danielsson jan.danielsson at gmail.com
Sat Jul 23 11:03:08 EDT 2005


Hello all,

   I have written a simple whiteboard application. In my application, I
want to be able to set draw attributes. This part works. I have a
dictionary object which contains stuff like:
self.attr['Pen.Color'] = ...
self.attr['Pen.Thickness'] = ...

   Now, the problem is that I want to be able to store attributes in a
list so they'll be easily accessed using the function keys. I.e. I have
the "current attributes" which I want to be able to store or retrieve
in/from a list,

The problem is that I have initialized the list like this:

self.drawAttr = { blah, blah, blah.. }
self.storedAttr = [ ]
for i in range(0, 10):
   self.storedAttr.append(self.drawAttr)

   I know what the problem is; they are all referencing the *same*
dictionary object. So, my question is: How do I initialize a list of
dictionary objects, where each list entry is its own object (which is a
copy from the self.drawAttr object).

Also, how do I store/restore entries to the list?

   I have found the "copy" module, and it's copy method. I assume this
would work:

for i in range(0, 10):
   self.storedAttr.append(copy.copy(self.drawAttr))

   However, the concept of "deep copy" confuses me. Do I want it, or
don't I want it? I repeat: the attributes object is a simple dictionary.

Thankful for any advice.



More information about the Python-list mailing list