Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

why is this invalid b = a += b ?

2 views
Skip to first unread message

Stef Mientki

unread,
Jan 24, 2009, 6:23:35 PM1/24/09
to pytho...@python.org
hello,

I can assign a value to more than 1 variable (name) in one line:

a = b = 3

So evaluation of this line must start at the right part.

But the following is not allowed:

b = 2
a = b += 1

I would think that if b has a value,
and the formula is evaluated from right to left,
there's nothing wrong with the above formula.

thanks
Stef Mientki

MRAB

unread,
Jan 24, 2009, 6:36:55 PM1/24/09
to pytho...@python.org
Assignment is a statement, not an expression. That's why:

if a = 0:
...

is illegal.

However, simple serial assignment such as:

a = b = 3

is useful enough to be supported as a special case.

Terry Reedy

unread,
Jan 24, 2009, 7:11:03 PM1/24/09
to pytho...@python.org
Stef Mientki wrote:
> hello,
>
> I can assign a value to more than 1 variable (name) in one line:
>
> a = b = 3
>
> So evaluation of this line must start at the right part.
>
> But the following is not allowed:
>
> b = 2
> a = b += 1

This strikes me as slightly incoherent. Given that v op=exp is mostly
that same as v = v op exp, I suppose you expect a = b += 1 to be mostly
the same as a = b = b+1, but what would you do with a += b = 1, or even
a *= b += 1. There is some virtur to restricting augmented assignment
to one target and one delimiter. Even that seems to cause some trouble.

tjr

0 new messages