-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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. |
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
* main: fixes pythongh-96078: os.sched_yield release the GIL while calling sched_yield(2). (pythongh-97965) pythongh-65961: Do not rely solely on `__cached__` (pythonGH-97990) pythongh-97850: Remove the open issues section from the import reference (python#97935) Docs: pin sphinx-lint (pythonGH-97992) pythongh-94590: add signatures to operator itemgetter, attrgetter, methodcaller (python#94591) Add Pynche's move to the What's new in 3.11 (python#97974) pythongh-97781: Apply changes from importlib_metadata 5. (pythonGH-97785) pythongh-86482: Document assignment expression need for ()s (python#23291) pythongh-97943: PyFunction_GetAnnotations should return a borrowed reference. (python#97949) pythongh-94808: Coverage: Test that maximum indentation level is handled (python#95926)
* main: pythonGH-97002: Prevent `_PyInterpreterFrame`s from backing more than one `PyFrameObject` (pythonGH-97996) pythongh-97973: Return all necessary information from the tokenizer (pythonGH-97984) fixes pythongh-96078: os.sched_yield release the GIL while calling sched_yield(2). (pythongh-97965) pythongh-65961: Do not rely solely on `__cached__` (pythonGH-97990) pythongh-97850: Remove the open issues section from the import reference (python#97935) Docs: pin sphinx-lint (pythonGH-97992) pythongh-94590: add signatures to operator itemgetter, attrgetter, methodcaller (python#94591) Add Pynche's move to the What's new in 3.11 (python#97974) pythongh-97781: Apply changes from importlib_metadata 5. (pythonGH-97785) pythongh-86482: Document assignment expression need for ()s (python#23291) pythongh-97943: PyFunction_GetAnnotations should return a borrowed reference. (python#97949)
…3291) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Thanks, looks like we've completed this |
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: