[Python-ideas] A comprehension scope issue in PEP 572
Nick Coghlan
ncoghlan at gmail.com
Sun May 6 23:38:25 EDT 2018
On 7 May 2018 at 13:15, Tim Peters <tim.peters at gmail.com> wrote:
> [Tim]
> >> There's a difference, though: if `y` "leaks", BFD. Who cares? ;-)
> >> If `y` remains inaccessible, there's no way around that.
> > Part of it is just that people seem to be fighting for the sake of
> > fighting. I'm weary of it, and I'm not going to debate this point with
> > you. You want 'em to leak? No problem. Implement it that way and I'm
> > not going to argue it.
>
> I'm more interested in real-life use cases than in arguments. My
> suggestion came from staring at my real-life use cases, where binding
> expressions in comprehensions would clearly be more useful if the
> names bound leaked. Nearly (but not all) of the time,, they're quite
> happy with that for-target names don't leak. Those are matters of
> observation rather than of argument.
>
The issue is that because name binding expressions are just ordinary
expressions, they can't be defined as "in comprehension scope they do X, in
other scopes they do Y" - they have to have consistent scoping semantics
regardless of where they appear.
However, it occurs to me that a nonlocal declaration clause could be
allowed in comprehension syntax, regardless of how any nested name bindings
are spelt:
p = rem = None
while any((rem := n % p) for p in small_primes nonlocal (p, rem)):
# p and rem were declared as nonlocal in the nested scope, so our
rem and p point to the last bound value
I don't really like that though, since it doesn't read as nicely as being
able to put the nonlocal declaration inline.
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/20180507/52da183d/attachment.html>
More information about the Python-ideas
mailing list