<div dir="ltr"><div>Thank you all. I will accept the PEP as is. I am happy to accept *clarification* updates to the PEP if people care to submit them as PRs to the peps repo (<a href="https://github.com/python/peps">https://github.com/python/peps</a>), and that could even (to some extent) include summaries of discussion we've had, or outright rejected ideas. But even without any of those I think the PEP is very clear so I will not wait very long (maybe a week).<br></div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Jul 2, 2018 at 8:38 AM Steven D'Aprano <<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Wed, Jun 27, 2018 at 07:29:52PM -0500, Tim Peters wrote:<br>
[...]<br>
> For example, if the name is declared "global" in the outer scope, you'll<br>
> get a compile-time error if you try to declare it "nonlocal" in the<br>
> contained scope.  "parentlocal" adjusts its meaning accordingly, becoming a<br>
> synonym for "global" in that specific case.<br>
<br>
"Parentlocal" is only a thing if we buy into the paradigm that inside <br>
comprehensions is a separate "local". And *that* is only true under <br>
two circumstances:<br>
<br>
- if you are utterly immersed in the implementation of comprehensions<br>
  as invisible, implicit functions;<br>
<br>
- or if you start from the premise that comprehensions ought to<br>
  encapsulate not just the loop variable, but anything else as well.<br>
<br>
<br>
But experimenting with locals() inside comprehensions shows that <br>
comprehension-scope *isn't* a well-defined thing. It already bleeds out <br>
of the comprehension, and so would some (but only some!) assignment <br>
expressions.<br>
<br>
Instead, if we start from the premise that comprehensions (like any <br>
other expression) run in the current scope, then there is no need to <br>
invent a term "parentlocal". There's just the usual LEGB scopes, plus <br>
class (which people usually forget).<br>
<br>
With no sublocal scopes (a term we never even had prior to this PEP) <br>
assignments inside the comprehension are no more special than <br>
assignments inside any other expression. They bind in the current scope, <br>
same as always, and keep the sensible identity that these two <br>
expressions are exactly equivalent in their visible semantics:<br>
<br>
    [x:=0, x:=1, x:=2]<br>
<br>
    [x:=i for i in (0, 1, 2)]<br>
<br>
including assignments.<br>
<br>
What about the loop variable?<br>
<br>
They ARE special, which is completely justified by the Zen:<br>
<br>
Although practicality beats purity.<br>
<br>
We can take a series of ever-more-detailed explanations, starting from <br>
the highest "bird's eye" view and gradually dropping further into the <br>
murky details of the implementation when, and if, required:<br>
<br>
- assignment within comprehensions is no different from assignment<br>
  in any other expression, it occurs in the local scope;<br>
<br>
- loop variables? they're a special case, for good reason, and are<br>
  encapsulated inside the comprehension;<br>
<br>
- how? they're hidden in an implicit, invisible scope, same as .0 <br>
  the implicit, invisible iterator object;<br>
<br>
- oh, you didn't know about the .0 variable? well forget about it,<br>
  it's an undocumented implementation detail, just like the invisible,<br>
  implicit function used by comprehensions;<br>
<br>
- oh, you didn't know about that either? read the source code.<br>
<br>
<br>
Only the first two levels of explanation are part of Python the <br>
language. The rest is CPython implementation.<br>
<br>
<br>
<br>
-- <br>
Steve<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/guido%40python.org" rel="noreferrer" target="_blank">https://mail.python.org/mailman/options/python-dev/guido%40python.org</a><br>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido">python.org/~guido</a>)</div>