preferred syntax for list extend?

Alex Martelli aleax at aleax.it
Wed Apr 17 16:55:01 EDT 2002


Ian Bicking wrote:
        ...
> But that's weird...
> 
> q1 += q3
> is not equivalent to:
> q1 = q1 + q3

If it were, there would be little reason for it to exist -- saving
a couple of keystrokes would be a silly reason to introduce a
bunch of new operators.  q1 = (whatever) *MUST* rebind q1,
this is fundamental Python semantics.  q1 += (whatever) MAY
rebind q1, when q1 was referring to an immutable object (no
inplace operations then), but it was introduced mostly to ease
*inplace* operations (on, obviously, *mutable* objects only) -- it
gains extra power by working polymorphically on both mutable
and immutable objects, but of course it cannot have _exactly_
identical semantics in the two cases (it would be useless if it
didn't take the opportunity to mutate a mutable, impossible if
it DID mutate immutables:-).


Alex




More information about the Python-list mailing list