preferred syntax for list extend?

Joshua Marshall jmarshal at mathworks.com
Wed Apr 17 17:10:47 EDT 2002


Alex Martelli <aleax at aleax.it> wrote:
> 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.

To me there's a more compelling reason for the existence of "+=": you don't need
to use a temporary variable to prevent the lvalue from being computed twice.
Eg:

  x[f()] += 1

vs

  x[f()] = x[f()] + 1

or

  idx = f()
  x[idx] = x[idx] + 1

Granted, since in Python assignment is a statement and not an expression, this
is less of a win than the existence of += in C, where it might be inconvenient
to introduce a temporary.



More information about the Python-list mailing list