List of lists surprising behaviour
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Fri Jun 18 11:03:34 EDT 2010
On Fri, 18 Jun 2010 12:07:38 +0100, bart.c wrote:
> (Although I have an issue with the way that that append works. I tried
> it in another, simpler language (which always does deep copies):
>
> L:=(1,2,3)
> L append:= L
> print L
>
> output: (1,2,3,(1,2,3))
>
> which is exactly what I'd expect,
> and not (1,2,3,(1,2,3,(1,2,3,...))) )
I find that behaviour a big surprise. You asked to append the list L, not
a copy of the list L. So why is this "simpler" language making a copy
without being asked?
If you asked for:
L:=(1,2,3)
M:=(0,1)
M append:= L
does it also append a copy of L instead of L? If so, how do you append
the original rather than wastefully making a copy? If L is huge, making a
copy before appending will be slow, and potentially fail.
--
Steven
More information about the Python-list
mailing list