Peculiar swap behavior
Aaron Brady
castironpi at gmail.com
Thu Mar 5 04:20:49 EST 2009
On Feb 23, 12:43 pm, Tim Chase <python.l... at tim.thechases.com> wrote:
> I stumbled across this oddity and was hoping folks on the list
> might be able to provide a little understanding:
>
> # swap scalars
> >>> x,y = 1,2
> >>> x,y = y,x
> >>> x,y
> (2, 1)
>
> # swap lists
> >>> a,b = [1,2,3],[4,5,6]
> >>> a,b = b,a
> >>> a,b
> ([4, 5, 6], [1, 2, 3])
>
> # swap list contents...not so much...
> >>> m,n = [1,2,3],[4,5,6]
> >>> m[:],n[:] = n,m
> >>> m,n
> ([4, 5, 6], [4, 5, 6])
snip
This may help:
>>> a= [1,2,3]
>>> id(a)
12170584
>>> id(a[:])
12235520
However, I'm not thinking in English at the moment.
More information about the Python-list
mailing list