New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Walrus Operator in list index #86482
Comments
|
Reading the PEP-572 document I don't see anything stating that Walrus operator in list indexes must be enclosed in parenthesis. Minimal Example: In [2]: idx = -1 In [3]: a[idx := idx +1] ''' If you enclose in parenthesis it works as expected: In [5]: a[(idx := idx +1)] In [6]: a[(idx := idx +1)] Is this a bug or am I misreading the PEP-572 document somewhere? It's not a top-level assignment nor is it a list comprehension so I would expect it to work in the first example. |
|
PEP-572 does not saw much of anything about when parens are needed. Nor does the low priority itself. Looking through the grammar of expressions, an assignment_expression is also a starred_expression, a positional_item (in calls), and the first part of a comprehension. It is not itself an plain expression unless and until wrapped in parentheses. Subscription requires an expression list, which may be a single expression. To put it another way: an expression is an assignment expression but an assignment expression with a "name :=" prefix is not an expression, so parens are required anywhere an expression is required. That includes subscripts, slicings, displays, call items other than positional items, comprehension parts other than the initial expression (maybe, check), parts of conditional expressions (maybe, check), and lambda expression bodies. After checking, I would like a sentence added to the doc before the 'go see the PEP' bit. This has nothing to do with asyncio. |


Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: