[Python-ideas] Unpacking iterables for augmented assignment

James Lu jamtlu at gmail.com
Sun Aug 26 12:29:48 EDT 2018


Steve and Johnathan, it seems like we're on the same page.

Currently, is <expr1> = <expr2> = <expr3> = <expr4> always equivalent
to <expr1> = <expr4>; <expr2> = <expr4>; <expr3> = <expr4>?

When there are a tuple or list of names on the left hand side (ex.
`a, b` or `(a, b)` or `[a, b]`), unpack the right hand side into
values and perform the augmented assignment on each name on the left
hand side. Borrowing Johnathan's example:
a = b = 0
incr = (1, 2)
a, b += incr
# now (a, b) == (1, 2)

concat = (-1, 0)
concat += incr
# existing behavior: now concat == (1, 2, 3, 4)

Like Steve said, you can save vertical space and avoid creating
temporary variables:

temp_a, temp_b = simulate(new_deck)
tally_a += temp_a
tally_b += temp_b

tally_a, tally_b = simulate(new_deck
)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180826/bdc1a900/attachment.html>


More information about the Python-ideas mailing list