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

Tim Peters tim.peters at gmail.com
Sun May 13 17:30:21 EDT 2018


[Tim]
> ...
> - If the target is not local to any function enclosing F, and is not
> declared `global` in the block containing F, then the target is bound
> in the block containing F.

FYI, that's still not right, but I've been distracted by trying to
convince myself that the manual actually defines what happens when
absurdly deeply nested functions mix local values for a name at some
levels with a `global` declaration of the name at other levels.

I suspect that the above should be reworded to the simpler:

- If the target is not  declared `global` or `nonlocal` in the block
  containing F, then the target is bound in the block containing F.

That makes "intuitive sense" because if the target is declared
`global` or `nonlocal` the meaning of binding in the block is already
defined to affect a not-local scope, while if it's not declared at all
then binding in the block "should" establish that it's local.to the
block (regardless of how containing scopes treat the same name)

But whether that all follows from what the manual already says
requires more staring at it ;-)

Regardless, if anyone were to point it out, I'd agree that it _should_
count against this that establishing which names are local to a block
may require searching top-level comprehensions in the block for
assignment expressions.  On a scale of minus a million to plus a
million, I'd only weight that in the negative thousands, though ;-)


More information about the Python-ideas mailing list