[Python-ideas] A comprehension scope issue in PEP 572

Nick Coghlan ncoghlan at gmail.com
Thu May 10 07:02:40 EDT 2018


On 9 May 2018 at 03:57, Tim Peters <tim.peters at gmail.com> wrote:

> These all match my expectations.  Some glosses:
>
> [Guido]
> > We should probably define what happens when you write [p := p for p in
> > range(10)]. I propose that this overwrites the loop control variable
> rather
> > than creating a second p in the containing scope -- either way it's
> probably
> > a typo anyway.
>
> A compile-time error would be fine by me too.  Creating two meanings
> for `p` is nuts - pick one in case of conflict.  I suggested before
> that the first person with a real use case for this silliness should
> get the meaning their use case needs, but nobody bit, so "it's local
> then" is fine.
>

I'd suggest that the handling of conflicting global and nonlocal
declarations provides a good precedent here:

>>> def f():
...     global x
...     nonlocal x
...     x = 1
...
  File "<stdin>", line 2
SyntaxError: name 'x' is nonlocal and global

Since using a name as a binding target *and* as the iteration variable
would effectively be declaring it as both local and nonlocal, or as local
and global.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180510/3af7424f/attachment.html>


More information about the Python-ideas mailing list