New submission from Brandon <[email protected]>:
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 [1]: a = list(range(10))
In [2]: idx = -1
In [3]: a[idx := idx +1]
File "<ipython-input-3-65fc931e77dd>", line 1
a[idx := idx +1]
^
SyntaxError: invalid syntax
'''
If you enclose in parenthesis it works as expected:
'''
In [4]: a[(idx := idx +1)]
Out[4]: 0
In [5]: a[(idx := idx +1)]
Out[5]: 1
In [6]: a[(idx := idx +1)]
Out[6]: 2
'''
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.
----------
components: asyncio
messages: 380691
nosy: Brando753, asvetlov, yselivanov
priority: normal
severity: normal
status: open
title: Walrus Operator in list index
versions: Python 3.8
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue42316>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com