<div dir="ltr"><div class="gmail_default" style="color:#000000"><span style="color:rgb(34,34,34)">On Mon, Jun 11, 2018 at 3:10 PM Rob Cliffe via Python-Dev <<a href="mailto:python-dev@python.org">python-dev@python.org</a>> wrote:</span><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Skip, I think you have misunderstood the point I was making. It was <br>
not whether the loop variable should leak out of a list comprehension. <br>
Rather, it was whether a local variable should, so to speak, "leak into" <br>
a list comprehension. And the answer is: it depends on whether the code <br>
is executed normally, or via exec/eval. Example:<br>
<br>
def Test():<br>
x = 1<br>
print([x+i for i in range(1,3)]) # Prints [2,3]<br>
exec('print([x+i for i in range(1,3)])') # Raises NameError (x)<br>
Test()<br>
<br>
I (at least at first) found the difference in behaviour surprising.<br></blockquote><div><br></div><div class="gmail_default" style="color:rgb(0,0,0)">Change 'def' to 'class' and run it again. You'll be even more surprised.</div></div></div>