Search code examples
pythondestructuringaugmented-assignment

Why is a destructuring augmented assignment not possible?


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?


Solution

  • 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.