On 7/6/06, <b class="gmail_sendername">Evan Simpson</b> &lt;<a href="mailto:evan@4-am.com">evan@4-am.com</a>&gt; wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Talin wrote:<br>&gt; I propose to create a new type of scoping rule, which I will call<br>&gt; &quot;explicit&quot; lexical scoping, that will co-exist with the current<br>&gt; &quot;implicit&quot; scoping rule that exists in Python today.
<br><br>I'd like to toss one more variant into the mix.&nbsp;&nbsp;If we really need to<br>address variables in an intermediate scope, the most explicit way that I<br>can think of doing so is to write (using Philip's example):<br><br>
def counter(num):<br>&nbsp;&nbsp;&nbsp;&nbsp;scope as outer # &quot;outer&quot; is an arbitrary identifier<br>&nbsp;&nbsp;&nbsp;&nbsp;def inc():<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outer.num += 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return outer.num<br>&nbsp;&nbsp;&nbsp;&nbsp;return inc<br></blockquote></div><br><br><br>Why not extend the interface to the locals builtin and add a __getitem__ that returns a proxy to access locals defined in other lexical scopes via __{get/set/del}attr_:
<br><br>def counter(num):<br>&nbsp;&nbsp;&nbsp; num = 1<br>&nbsp;&nbsp;&nbsp;&nbsp;def inc():<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; locals[1].num += 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return outer.num<br>&nbsp;&nbsp;&nbsp;&nbsp;return inc<br><br><br>Where, for CPython, locals[n] gives access to NamespaceProxy(sys._getframe(n).f_locals).&nbsp; In addition to having a relatively pleasing and explicit syntax, this may be a feasible method for allowing portable introspection into outer scopes without having to export the whole frame object a la sys._getframe(n).&nbsp; I strongly suspect that Jython, IronPython, and PyPy would have little difficulty supporting (and optimizing) this construct.
<br><br>Lacking core language support, it is easy to roll an object that does just what I suggest.&nbsp; Actual implementation is left to a more motivated reader, of course.<br><br>Just another crazy idea to throw into the pot.
<br><br>-Kevin<br><br>