Lists & "pointers"

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sun Jul 24 05:16:43 CEST 2005


On Sat, 23 Jul 2005 17:03:08 +0200, Jan Danielsson wrote:

> 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).

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

You only need to worry about the difference between copy and deepcopy if
the objects inside the dict are complex objects like dicts and lists.

You also said that: "I want to be able to store attributes in a list so
they'll be easily accessed using the function keys."

I don't think this is good usage. What happens when you change the
attributes in one place but forget to change it in the other?

A better solution would be to set up either a list or a mapping from
function key to attribute, rather than to a COPY of the attribute. Why
change things in two places rather than one?

Something like this:

# set up attributes before hand
self.attr['Pen.Color'] = 'blue'
self.attr['Pen.Thickness'] = 1
self.attr['Pen.State'] = 'down'
# etc
# now point the function keys to attributes
self.functionkeys = {'F1' = 'Pen.Color', 'F2' = 'Pen.Thickness', 
    'F3' = 'Pen.State', ... }

Then, when you want to access the current value of some attribute, instead
of looking up a list:

# bad way
def get_attribute(fkey):
    if fkey = 'F1':
        return self.storedAttr[0]
    elif fkey = 'F2':
        return self.storedAttr[1]
    ...
    elif fkey = 'F12':
        return self.storedAttr[11]

you would do something like this:
    
# good way
def get_attribute(fkey):
    return self.attr[self.functionkeys[fkey]]



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

That question is awfully open-ended. Can you be more specific?



>    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.

That depends on what is inside your simple dictionary. For immutable
objects like ints, floats and strings, copy is sufficient:

>>> D1 = {1: 'hello', 2: 'there'}
>>> D1
{1: 'hello', 2: 'there'}
>>> D2 = D1.copy()
>>> D1[1] = 'go'
>>> D1
{1: 'go', 2: 'there'}
>>> D2
{1: 'hello', 2: 'there'}

See what happens when the values are mutable objects:

>>> DM1 = {1: [0,1], 2: [4, 5]}
>>> DM2 = DM1.copy()
>>> DM1[3] = [0,2]
>>> DM1
{1: [0, 1], 2: [4, 5], 3: [0, 2]}
>>> DM2
{1: [0, 1], 2: [4, 5]}

So far so good. But now look:

>>> DM1[1].append(999)
>>> DM1
{1: [0, 1, 999], 2: [4, 5], 3: [0, 2]}
>>> DM2
{1: [0, 1, 999], 2: [4, 5]}

The difference is that although copy makes a copy of the top level of the
dict, it DOESN'T make copies of the individual objects within the dict.  

This doesn't matter is the objects are immutable, but if they are lists or
other dicts, you can get surprises like the above.


-- 
Steven.




More information about the Python-list mailing list