<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Jan 20, 2016 at 4:10 PM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
</span>I'm just tossing the "static block" idea out for discussion, but if you<br>
want a justification here are two differences between capture/static<br>
and global/nonlocal which suggest they aren't that similar and so we<br>
shouldn't feel obliged to use the same syntax.<br>
<br>
(1) global and nonlocal operate on *names*, not values. E.g. after<br>
"global x", x refers to a name in the global scope, not the local scope.<br>
<br>
But "capture"/"static" doesn't affect the name, or the scope that x<br>
belongs to. x is still a local, it just gets pre-initialised to the<br>
value of x in the enclosing scope. That makes it more of a binding<br>
operation or assignment than a declaration.<br>
<br>
(2) If we limit this to only capturing the same name, then we can only<br>
write (say) "static x", and that does look like a declaration. But maybe<br>
we want to allow the local name to differ from the global name:<br>
<br>
    static x = y<br>
<br>
or even arbitrary expressions on the right:<br>
<br>
    static x = x + 1<br>
<br>
Now that starts to look more like it should be in a block of code,<br>
especially if you have a lot of them:<br>
<br>
    static x = x + 1<br>
    static len = len<br>
    static data = open("data.txt").read()<br>
<br>
versus:<br>
<br>
    static:<br>
        x = x + 1<br>
        len = len<br>
        data = open("data.txt").read()<br>
<br>
<br>
I acknowledge that this goes beyond what the OP asked for, and I think<br>
that YAGNI is a reasonable response to the static block idea. I'm not<br>
going to champion it any further unless there's a bunch of interest from<br>
others.</blockquote><div><br></div><div>Yeah, your arguments why it's different from global/nonlocal are reasonable, but the question remains whether we really need all that functionality. IIRC C++ lambdas only allow capturing a variable's value, not an expression's.<br><br>So we should ask ourselves first: if we *only* had some directive that captures some variables' values, essentially like the len=len argument trick but without affecting the signature (e.g. just "static x, y, z"), how much of the current pain would be addressed, and how much would remain?<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">(I'm saving my energy for Eiffel-like require/ensure blocks<br>
*wink*).<br></blockquote><div><br></div><div>Now you're making me curious. <br></div></div><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div></div>