<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 25 March 2018 at 02:34, Eric Fahlgren <span dir="ltr"><<a href="mailto:ericfahlgren@gmail.com" target="_blank">ericfahlgren@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><span class=""><div style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)">On Sat, Mar 24, 2018 at 7:14 AM, Nick Coghlan </span><span dir="ltr" style="color:rgb(34,34,34)"><<a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>></span><span style="color:rgb(34,34,34)"> wrote:</span></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div style="color:rgb(0,0,0);display:inline"></div> >>> class C:<br> ... sequence = range(10)<br> ... listcomp = [x for x in sequence]<br></div></div></div></blockquote><div> </div></div></div></span><div class="gmail_extra"><div style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">>>> class C:</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">
<span style="font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration-style:initial;text-decoration-color:initial;color:rgb(34,34,34);background-color:rgb(255,255,255);float:none;display:inline">... y = 1</span><br style="font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration-style:initial;text-decoration-color:initial;color:rgb(34,34,34);background-color:rgb(255,255,255)">
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">
<span style="font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration-style:initial;text-decoration-color:initial;color:rgb(34,34,34);background-color:rgb(255,255,255);float:none;display:inline">... sequence = range(10)</span><br style="font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration-style:initial;text-decoration-color:initial;color:rgb(34,34,34);background-color:rgb(255,255,255)">
... listcomp = [x+y for x in sequence]</span>
</div><div style="color:rgb(0,0,0)">...</div><span class=""><div style="color:rgb(0,0,0)">Traceback (most recent call last):</div><div style="color:rgb(0,0,0)"> File "<stdin>", line 1, in <module></div></span><div style="color:rgb(0,0,0)"> File "<stdin>", line 4, in C</div><div style="color:rgb(0,0,0)"> File "<stdin>", line 4, in <listcomp></div><div style="color:rgb(0,0,0)">NameError: name 'y' is not defined<br></div><br></div><div class="gmail_extra"><div style="color:rgb(0,0,0)">Ok, so how does 'y' fit into these scoping rules?</div></div></div></blockquote><div><br></div><div>Everything except the outermost iterator is evaluated in the implicitly nested scope, so comprehensions at class scope have the restriction that only the outermost iterator can access names defined in the class scope. It turned out that was enough to preserve compatibility with most of the comprehensions that folks actually use at class scope.<br><br>For those rare cares where it isn't, the typical resolution is to either define a helper function, or else switch to a regular for loop.<br></div><div><br></div><div>Cheers,<br></div><div>Nick.<br></div></div><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Nick Coghlan | <a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a> | Brisbane, Australia</div>
</div></div>