<div dir="ltr"><div><div class="gmail_signature"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><br></div></div></div></div></div></div></div></div></div></div></div><br><div class="gmail_quote">On Mon, May 2, 2016 at 8:21 AM, Random832 <span dir="ltr"><<a href="mailto:random832@fastmail.com" target="_blank">random832@fastmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><span class="">On Mon, May 2, 2016, at 10:35, Bruce Leban wrote:<br>> Using a single letter (or short) variable name works well most of the<br>> time<br>> but has one problem: the variable can leak. It's easy to forget to write<br>> the del statement. Imagine a block statement that had the effect of<br>> deleting any variables initialized in the block.<br><snip><br></span><br>Should it delete the variables, or should it be a new scope? They're<br>subtly different.<br><br>If the former, how do you differentiate a variable initialized in the<br>block from a variable assigned in the block? What if you really *do*<br>want to keep one of them? (The answer in both cases if it's a new scope<br>would be to use "nonlocal")</blockquote><div><br></div><div>Fair question. It's not a new scope because it's too clumsy to have to declare every variable outside the block nonlocal. If a variable X is not local to the enclosing scope (or declared global), then it's local to the block. I don't think you can do this at runtime; I think it has to be determined at compile time. Consider:</div><div><br></div><div> def foo(i):</div><div> if i:</div><div> x = 1</div><div> with:</div><div> x = 2</div><div> y = 3</div><div><br></div><div>This should not del x regardless of the value of i. In the following case the variable in the block affects the outer scope. Consider:</div><div><br></div><div><div> def foo(i):</div><div> with:<br></div><div> y = 3<br></div><div> return y</div></div><div><br></div><div>Treating this as</div><div><br></div><div><div> def foo(i):</div><div> y = 3<br></div><div> del y</div><div> return y<br></div></div><div><br></div><div>has the right result (unbound error). Without the block return y would be a reference to a global variable.</div><div> </div></div><div class="gmail_extra">
<br><div class="gmail_quote">On Mon, May 2, 2016 at 5:06 PM, Eric Snow <span dir="ltr"><<a href="mailto:ericsnowcurrently@gmail.com" target="_blank">ericsnowcurrently@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><span class="">On Mon, May 2, 2016 at 8:35 AM, Bruce Leban <<a href="mailto:bruce@leban.us">bruce@leban.us</a>> wrote:<br>
> Using a single letter (or short) variable name works well most of the time<br><snip><br>
><br>
> x = foo()<br>
> with:<br>
> y = x + 1<br>
> x = y + 2<br>
><br><br>
</span>Hmm. That reminds me of the "given" syntax (PEP 3150:<br>
<a href="https://www.python.org/dev/peps/pep-3150/" rel="noreferrer" target="_blank">https://www.python.org/dev/peps/pep-3150/</a>). This is like allowing<br>
blocks of code to be treated as first-order, a la Ruby. Then again,<br>
functions give us just about all we need, and the "given" syntax<br>
basically gives us multi-line lambdas. :) Perhaps it's time to dust<br>
off that proposal.<span class=""><font color="#888888"><br>
</font></span></blockquote></div></div><div><br></div>There is a difference. "given" or whatever it's called requires the variables to be declared and initialized at the top, while this would automatically delete any references created in the block.<div><br><div class="gmail_extra"> </div><div class="gmail_extra"><br class="">On Mon, May 2, 2016 at 8:31 AM, Guido van Rossum <span dir="ltr"><<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="">The choice of keyword is not entirely arbitrary; if we can't come up with a decent keyword the feature is dead. But the introduction would have to include a `from __future__ import <something>` statement anyway for at least one, maybe two release cycles (we did this with the `with` statement itself, around 2.4/2.5). So Django will have plenty of time to change.<br></span></div><div><span class=""><br></span></div></div></div></blockquote><div><br></div><div>Note that the "auto-deleting block" (for want of a better descriptive name) could be introduced with a contextual keyword. Currently</div><div><br></div><div> <identifier> : </div><div><br></div><div>is not valid syntax so any identifier could be used for these blocks without requiring it to be recognized as a keyword in other contexts.</div><div> </div><div>--- Bruce</div></div></div></div>