References vs copies w/ *x

Zamboo despamme.bstankie at eye.psych.umn.edu
Fri Oct 1 03:12:01 EDT 1999


Hello,

I have read through the Learning Python book and have found some peculiar
behavior with the *x (repetition) command.  Here is what I am doing and
getting:

>>> a=[1,2,3]
>>> b=[a[:]]*3
>>> b
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> b[0][2]=10
>>> b
[[1, 2, 10], [1, 2, 10], [1, 2, 10]]


Notice that if I change one of the elements in b, it changes all of the
elements.  I assume that the repetition is also referencing a memory
location, but how can I force the repetition command to make copies?

Thanks,
Brian

-- 
<if you want to e-mail me please de-spam my address>




More information about the Python-list mailing list