What does x[:]=[4,5,6] mean?
Alex Martelli
aleaxit at yahoo.com
Mon Jun 4 15:29:43 EDT 2001
"Jonathan Gardner" <gardner at cardomain.com> wrote in message
news:9fgm2k$at7$1 at brokaw.wa.com...
...
> The bottom line is the assignment operator _never_ makes a copy of
> something - the end result is to make the variables point to the object on
> the right hand side.
Hmmm... assignment to a _slice_ may be slightly different in
this regard, so, given the subject of this thread, take care...
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> a[:]=b
>>> c=b
>>> b[1]=99
>>> a
[4, 5, 6]
>>> b
[4, 99, 6]
>>> c
[4, 99, 6]
>>>
See? The a[:]=b *has* "made a copy of something" -- quite
different from the c=b. No references to the object bound to
b have been added by the slice assignment -- rather, the items
have been copied. You could say it's the work of a's __setitem__
(or built-in list's equivalent thereof) when it receives a slice
argument, but it may be simpler to explain as the assignment
doing the copy in this case, I think.
Alex
More information about the Python-list
mailing list