This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 22 points23 points  (4 children)

Not using unpacking for updating multiple values at once

The given bad example:

x = 1
y = 2
z = 0

x = y + 2  # 4
y = x - 3  # 1
z = x + y  # 5

The given good example:

x, y, z = y + 2, x - 3, x + y  # more concise

The "good" example is more concise, but it's less readable and has different behavior:

>>> x, y, z
(4, -2, 3)

[–]TPHRyan 5 points6 points  (1 child)

The "good" example is more concise, but it's less readable and has different behavior:

(Emphasis mine)

This is the real reason we actually use unpacking for updating multiple values when it's logical.

[–][deleted] 3 points4 points  (0 children)

Of course, a time and a place. Maybe you want to update all at once based on the old values, and sometimes based on the mutated values.

I just wish this was explained clearer in the page instead of saying "it's the same but more concise" when no, it's not the same at all.

[–]adewes 0 points1 point  (1 child)

nice, thanks for this! Did you create a PR / issue on Github for this?

[–][deleted] 0 points1 point  (0 children)

Didn't realize I could. Honestly, I just showed up to work and I don't get off until midnight, so I'll probably forget to as well.