[Python-ideas] A comprehension scope issue in PEP 572
tim.peters at gmail.com
Mon May 7 00:04:41 EDT 2018
[Nick Coghlan <ncoghlan at gmail.com>]
> 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.
While I'm not generally a fan of arguments, I have to concede that's a
really good argument :-)
Of course their definition _could_ be context-dependent, but even I'll
agree they shouldn't be. Never mind!
> However, it occurs to me that a nonlocal declaration clause could be allowed
> in comprehension syntax, regardless of how any nested name bindings are
> 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.
If the idea gets traction, I'm sure we'll see 100 other syntax ideas
by the time I wake up again.
More information about the Python-ideas