[Python-ideas] On evaluating features [was: Unpacking iterables for augmented assignment]

Chris Angelico rosuav at gmail.com
Tue Aug 28 04:15:42 EDT 2018


On Tue, Aug 28, 2018 at 6:05 PM, Stephen J. Turnbull
<turnbull.stephen.fw at u.tsukuba.ac.jp> wrote:
> In the case in point, the destructuring assignments
>
>     a, b = b, a
>     w, x, y, z = z, w, y, x
>
> can be interpreted as "swapping" or "permuting", and AIUI that's why
> they were included.  They express the intent better than
>
>     tmp = a
>     a = b
>     b = tmp
>     del tmp
>
> and I don't want to even think about how to do the 4-variable version
> without 4 temporary variables.  By comparison,
>
>     x, y += a, b
>
> is neither more expressive, nor easier to read, nor significantly
> harder to type, than
>
>     x += a
>     y += b
>
> as far as I can see.

When you have completely different variables, sure. But what if - like
in the swap example - they're the same variables?

def frobnicate():
    a, b, c = 1, 10, 100
    while True:
        a, b, c += b, c, a
        yield a

On the first iteration, this would be:

a += 10
b += 100
c += 1

I don't have an actual use-case, but to be fair, I also have very few
use-cases for swapping/permuting.

-0 on adding it, but it's not an illogical feature.

ChrisA


More information about the Python-ideas mailing list