Behavior of += (was Re: [Python-Dev] Customization docs)

John Roth johnroth at ameritech.net
Tue Jun 4 14:03:30 CEST 2002


"Huaiyu Zhu" <huaiyu at gauss.almadan.ibm.com> wrote in message
news:slrnafo7qv.37c.huaiyu at gauss.almadan.ibm.com...
> Gustavo Cordova <gcordova at hebmex.com> wrote:

>
> Great explanation!  The problem stems from the fact that the current
+=
> operator implements two semantically distinct operations, chosen
according
> to some properties of the operands.  This is not unlike the situation
of the
> division operator with distinct behaviors on floats and ints.

This is not an issue with '+='. It's an issue with assignment,
regardless
of whether it is standalone or combined with some other operation.

And I disagree that assignment implements two distinct operations.
Assignment always rebinds. Always. In the examples cited, it
attempted to rebind into a tuple, because that's what was asked
for when the left hand side had a subscript.

The reason that the examples given at the top of the thread fail
in such strange ways is simply that '+=' does not check whether
the assignment is possible before doing the addition (or concatination,
in the specific cases cited.)

To see this, you could try the first example, using explicit '+' and
'='. I suspect it would fail in exactly the same way, with exactly
the same side effect.

John Roth






More information about the Python-list mailing list