[issue38469] PEP 572 : assignment expression to a global variable in a comprehension

Pierre Quentel report at bugs.python.org
Sun Oct 13 16:54:11 EDT 2019


New submission from Pierre Quentel <pierre.quentel at gmail.com>:

PEP 572 says that "an assignment expression occurring in a (...) comprehension (...) binds the target in the containing scope, honoring a nonlocal or global declaration for the target in that scope, if one exists."

In Appendix B, the PEP shows this example :

def f():
    global TARGET
    a = [TARGET := EXPR for VAR in ITERABLE]

So I don't understand why this fails:

Python 3.8.0rc1 (tags/v3.8.0rc1:34214de, Oct  1 2019, 18:42:37) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 0
>>> def f():
...     global x
...     [x := i for i in range(5)]
...
  File "<stdin>", line 3
SyntaxError: no binding for nonlocal 'x' found
>>>

Is this a bug or am I missing something ?

----------
components: Interpreter Core
messages: 354601
nosy: quentel
priority: normal
severity: normal
status: open
title: PEP 572 : assignment expression to a global variable in a comprehension
type: behavior
versions: Python 3.8

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


More information about the Python-bugs-list mailing list