Why not a, b += i, j? (augmented assignment via tuple unpacking)

John Roth johnroth at ameritech.net
Wed Nov 27 14:22:46 EST 2002


"Joshua Marshall" <jmarshal at madmax.mathworks.com> wrote in message
news:as2ls4$2up$1 at ginger.mathworks.com...
> John Roth <johnroth at ameritech.net> wrote:
>
> > I'd think it would be equivalent to:
>
> > a += i
> > b += j
>
> > the corner case you mention should behave
> > the same as
>
> > a, b = b, a
>
> > Why make it more complicated than it has
> > to be?
>
> Because if you do the translation with your rule, you get:
>
>   a += b
>   b += a
>
> which is, in my opinion, unexpected.

That's the problem in a nutshell. If Python had a
pervasive rule for places where you can get
side effects from the order of updating, I'd say
to go with whatever that rule is. However, AFIK,
it doesn't, and what's intuitive to me, once I've
enunciated that it's equivalent to doing the bindings
one at a time, is that the order of doing them is
significant.

As you say, it depends on one's personal opinion.
I have no great attachment to either; but it would
have to be clearly documented, not left as something
that just happens that way.

John Roth





More information about the Python-list mailing list