quantifiedcode / python-anti-patterns

An open collection of Python anti-patterns and worst practices.
https://quantifiedcode.github.io/python-anti-patterns
Other
1.71k stars 249 forks source link

"Not using unpacking for updating multiple values at once" contains wrong information. #27

Closed justanr closed 9 years ago

justanr commented 9 years ago

The entry for using generalized unpacking for updating multiple variables at once has these examples:

The "bad" example:

x = 1
y = 2
z = 0

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

And the "good" example:

x = 1
y = 2
z = 0

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

And also makes the following claim:

The modified code below is functionally equivalent to the original code above, but this code is more concise.

Emphasis mine. However, the "good" example has different behavior as it uses the original values for each assignment:

x, y, z = y + 2, x - 3, x + y
print(x, y, z) # 4, -2, 3

I won't debate the "more concise" claim -- because it is -- but it's definitely less readable; however, adding parens goes a long way to delimited each update:

x, y, z = (y + 2), (x - 3), (x + y)
adewes commented 9 years ago

Thanks @justanr , I think someone already made a PR with a better example for this, I think we can close this issue for now. If you think it's not fixed yet feel free to open it again. I'll redeploy the new version of the book today so that the changes will be visible online.