1

Destructuring is possible in python:

a, b = 1, 2

Augmented assignment is also possible:

b += 1

But is there a reason destructuring augmented assignment cannot be done?:

a, b += 1, 2
> SyntaxError: illegal expression for augmented assignment

From what I can tell, destructuring is a language thing; it cannot be modified by something like object.__add__(). Why won't the language call object.__iadd__() on each part of the augmented assignment separately?

user2357112
  • 260,549
  • 28
  • 431
  • 505
Evan Benn
  • 1,571
  • 2
  • 14
  • 20
  • 2
    Personally, when I look at `a, b += 1, 2`, I'm not sure what it should do. `(a, b) + (1, 2)` equals `(1, 2, 1, 2)` so I would expect `(a, b) += (1, 2)` to be the same as `(a, b) = (a, b) + (1, 2)`, which equals `(a, b) = (1, 2, 1, 2)` but how do you unpack that? – Mark Jan 17 '19 at 05:13
  • Perhaps it's because the base expression `a,b = a,b + 1,2` is invalid. – John Gordon Jan 17 '19 at 05:15
  • These comments make sense, thank you. I guess then my question is, why is the augmented rule applied before the destructuring rule. `a, b += 1, 2 => a = a + 1; b = b + 2` And maybe the answer is, it might be confusing. – Evan Benn Jan 17 '19 at 05:31
  • 1
    It's unclear for me, how the expressions like the `a, b += 1, a` could be evaluated, if such a syntax is allowed. Should we evaluate `a += 1` first and get an `a'`, than `b += a'`, or just evaluate `a += 1`, `b += a`? Looks like it's an undefined behaviour. – Mikhail Stepanov Jan 17 '19 at 06:59
  • I think that @MikhailStepanov has found a good answer as to why this wouldn't work, thanks! – Evan Benn Jan 18 '19 at 02:03

1 Answers1

2

Probably it's because of undefined behaviour in expressions like a:

a, b += 1, a

How it should be evaluated? Like this

a' = a + 1
b = b + a'

or just

b = b + a
a = a + 1

- it's unclear. So, destructuring augmented assignment is not allowed.

Mikhail Stepanov
  • 3,680
  • 3
  • 23
  • 24