[issue44223] := in comprehensions does not work

Dennis Sweeney report at bugs.python.org
Mon May 24 03:27:35 EDT 2021


Dennis Sweeney <sweeney.dennis650 at gmail.com> added the comment:

The parser rejects this ambiguity and requires parentheses:
[
    xxx
    for item in collection
    if (xxx := mutator(item)) is not None
]

Example ambiguity:

>>> [x for item in "abcdefabc" if x := item.upper() not in "ABC"]
SyntaxError: invalid syntax
>>> [x for item in "abcdefabc" if (x := item.upper()) not in "ABC"]
['D', 'E', 'F']
>>> [x for item in "abcdefabc" if (x := item.upper() not in "ABC")]
[True, True, True]
>>>

----------
nosy: +Dennis Sweeney

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44223>
_______________________________________


More information about the Python-bugs-list mailing list