[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Concerning copy
Kamilche wrote:
You also need to obtain a COPY of a list, if you are deleting objects.
Otherwise you skip every other object, because the list size changes
as you iterate.
Huh?
>>> a = [1,2,3,4,5]
>>> b = a[:]
>>> del b[3]
>>> a
[1, 2, 3, 4, 5]
>>> b
[1, 2, 3, 5]
or
>>> c = ['avalanche', 'questionmark', 'spamcatcher', 'goofball']
>>> d = c[:]
>>> del d[3]
>>> c
['avalanche', 'questionmark', 'spamcatcher', 'goofball']
>>> d
['avalanche', 'questionmark', 'spamcatcher']
>>> for w in c:
... del w
...
>>> c
['avalanche', 'questionmark', 'spamcatcher', 'goofball']
What does that buy you? The objects will still be in the first list, because
there is still a reference to them. Deletion of elements of a list deletes
the references, not the objects themselves (objects go away when nothing
references them anymore).
You must be thinking of some different use case than this.
Cheers,
Terry
--
Terry Hancock (hancock@xxxxxxxxxxxxxxxxxxxx)
Anansi Spaceworks http://www.AnansiSpaceworks.com