Re: surprising behavior of chained initializations

Aaron Watters (aaron@funcity.njit.edu)
Wed, 1 Feb 1995 19:29:07 GMT

In article <D2zHzF.6nx@cgl.ucsf.edu> pett@ucsf.edu (Eric Pettersen) writes:
> I believe I understand the behavior of the following script, but I
>don't like it:
>>a = b = []
>>a.append("x")
>>b.append("y")
>>print b
> The output is "['x', 'y']". I think the naive observer would expect
>"['y']" as the output. Shouldn't the left-hand sides of multiple
>initializations get copies of the right-hand sides, instead of name aliases (or
>whatever)?

Emphatically not, they should get references, not copies.
It's much faster and cheaper that way, and you can *use*
the side effects, if you want.

Use l[:] to get a copy of the innards of a list.

On the otherhand, if you want the dangerous power of side effects,
use "in-place" operators like append and sort:
>>> l = k = [5,2,6,12,1]
>>> l.sort()
>>> k
[1, 2, 5, 6, 12]
Wow! I sorted k, and I didn't even raise a sweat!
-a.
===
I'm in complete solidarity with the proletariate.
I just prefer not to socialize with them, if you
know what i mean.