[Python-ideas] A comprehension scope issue in PEP 572
Jacco van Dorp
j.van.dorp at deonet.nl
Mon May 7 05:33:48 EDT 2018
For what it's worth, i'm totally +1 on inline uses of global and nonlocal.
As a related improvement, i'd also like it if "global x = 5" would be
a legal statement. As a noob learning python, I was suprised to find
out I couldn't and had to split it on two lines.(aside from a 9-hour
course of C and some labview (which I totally hate), python was my
first language and still the one im by far most proficient with.)
2018-05-07 6:04 GMT+02:00 Tim Peters <tim.peters at gmail.com>:
> [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.
> Python-ideas mailing list
> Python-ideas at python.org
> Code of Conduct: http://python.org/psf/codeofconduct/
More information about the Python-ideas