<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Mar 26, 2018 at 7:57 AM, Nick Coghlan <span dir="ltr"><<a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 26 March 2018 at 14:34, Guido van Rossum <<a href="mailto:guido@python.org">guido@python.org</a>> wrote:<br>
> Not so fast. There's a perfectly reasonable alternative to sublocal scopes<br>
> -- just let it assign to a local variable in the containing scope. That's<br>
> the same as what Python does for for-loop variables. Note that for<br>
> comprehensions it still happens to do the right thing (assuming we interpret<br>
> the comprehension's private local scope to be the containing scope).<br>
<br>
</span>I finally remembered one of the original reasons that allowing<br>
embedded assignment to target regular locals bothered me: it makes<br>
named subexpressions public members of the API if you use them at<br>
class or module scope. (I sent an off-list email to Chris about that<br>
yesterday, so the next update to the PEP is going to take it into<br>
account).<br>
<br>
Similarly, if you use a named subexpression in a generator or<br>
coroutine and it gets placed in the regular locals() namespace, then<br>
you've now made that reference live for as long as the generator or<br>
coroutine does, even if you never need it again.<br>
<br>
By contrast, the sublocals idea strives to keep the *lifecycle* impact<br>
of naming a subexpression as negligible as possible - while a named<br>
subexpression might live a little longer than it used to as an<br>
anonymous subexpression (or substantially longer in the case of<br>
compound statement headers), it still wouldn't survive past the end of<br>
the statement where it appeared.<br></blockquote><div><br></div><div>But this is not new: if you use a for-loop to initialize some class-level structure  you have the same problem. There is also a standard solution (just 'del' it).<br></div></div><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div></div>