<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Sun, Jun 24, 2018 at 2:10 PM Chris Angelico <<a href="mailto:rosuav@gmail.com">rosuav@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Mon, Jun 25, 2018 at 4:06 AM, Steven D'Aprano <<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>> wrote:<br>
><br>
> Remember, the driving use-case which started this (ever-so-long)<br>
> discussion was the ability to push data into a comprehension and then<br>
> update it on each iteration, something like this:<br>
><br>
>     x = initial_value()<br>
>     results = [x := transform(x, i) for i in sequence]<br>
<br>
Which means there is another option.<br>
<br>
5. Have the assignment be local to the comprehension, but the initial<br>
value of ANY variable is looked up from the surrounding scopes.<br>
<br>
That is: you will NEVER get UnboundLocalError from a<br>
comprehension/genexp; instead, you will look up the name as if it were<br>
in the surrounding scope, either getting a value or bombing with<br>
regular old NameError.<br>
<br>
Or possibly variations on this such as "the immediately surrounding<br>
scope only", rather than full name lookups. It'd have an awkward<br>
boundary somewhere, whichever way you do it.<br>
<br>
This isn't able to send information *out* of a comprehension, but it<br>
is able to send information *in*.<br></blockquote></div><div><br></div><div>But this "horrifies" me for a slightly different reason: it effectively introduces a new case of dynamic scoping, which Python used to do everywhere but has long switched away from, with the exception of class scopes (whose difference with function scopes sometimes confuses people -- usually people who put too much code in their class scope).<br></div><div><br></div>-- <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></div>