On Wed, Oct 21, 2009 at 8:47 AM, Masklinn <<a href="mailto:masklinn@masklinn.net">masklinn@masklinn.net</a>> wrote:<br>>... I do think `let` fits the Zen better than<br>> nothing/`global`/`nonlocal`:<br>...<br>

<div class="im">> instead of 3 different forms depending on the exact<br><br></div><div>First, I hardly think introducing a 'let' keyword as you suggest is pythonic. It's adding new syntax of a different flavor than the current syntax and breaking existing code.</div>

<div><br></div><div>Why is it a different flavor? Because apparently you didn't notice that global and nonlocal are statements in their own right, not modifications of an assignment statement. So you're suggesting:</div>

<div><br></div><div>    global one</div><div>    nonlocal two</div><div>    let three = value</div><div><br></div><div>It's also a different flavor because global says what it means, nonlocal says what it means and let says nothing about what it means.</div>

<div><br></div>And breaking existing code without a damn good reason? I don't see that happening.<div><br></div><div>It seems a bit inconsistent to me that global/nonlocal act differently with respect to reading and writing variables. That's a far worse problem than you're trying to solve. (See below.) But changing that would break a lot of code. So we live with it.</div>

<div><br></div><div>My proposal (whether or not it's a good idea) wouldn't break any existing code. The new syntax proposed is currently invalid so can't appear in existing programs. It would add a new local statement that is only recognized in contexts that don't exist right now (and therefore can't break existing programs).</div>

<div><br></div><div>The truth is that global and nonlocal add cognitive load to the person reading your code because they can't just read the one line to see what it's doing. This increases the burden on the developer to be clear in what they're writing. If my proposal allows us to decrease cognitive load on the reader, then it might be worth considering.</div>

<div><br></div><div>As an aside, if instead of using the global/nonlocal statement we wrote:</div><div><br></div><div>   global.one = nonlocal.two</div><div><br></div><div>Then the reader knows exactly what's global at the time it gets read. But I don't see that happening anytime soon given the preference for TOOWTDI and taking out the global/nonlocal statements would break too much code. And :-) we'd have to figure out what this statement means:</div>

<div><br></div><div>    foo = bar + global.bar + nonlocal.bar</div><div><br>--- Bruce<br><a href="http://www.vroospeak.com">http://www.vroospeak.com</a><br><br></div><div><br></div><div><div>>>> def f():</div><div>

<span class="Apple-tab-span" style="white-space:pre"> </span>def g():</div><div><span class="Apple-tab-span" style="white-space:pre">             </span>print(i)</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>i = 5</div>

<div><span class="Apple-tab-span" style="white-space:pre">      </span>g()</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div>>>> f()</div><div>5</div><div><br></div><div>

<div>>>> def f2():</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>def g():</div><div><span class="Apple-tab-span" style="white-space:pre">             </span>i = 5</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>g()</div>

<div><span class="Apple-tab-span" style="white-space:pre">      </span>print(i)</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">    </span></div><div>>>> f2()</div><div>Traceback (most recent call last):</div>

<div>  File "<pyshell#35>", line 1, in <module></div><div>    f2()</div><div>  File "<pyshell#34>", line 5, in f2</div><div>    print(i)</div><div>NameError: global name 'i' is not defined</div>

</div><div><br></div><div>>>> def f3():</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>i = None</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>def g():</div><div><span class="Apple-tab-span" style="white-space:pre">             </span>nonlocal i</div>

<div><span class="Apple-tab-span" style="white-space:pre">              </span>i = 5</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>g()</div><div><span class="Apple-tab-span" style="white-space:pre">  </span>print(i)</div>

<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">     </span></div><div>>>> f3()</div><div>5</div></div>