On 7/6/06, <b class="gmail_sendername">Evan Simpson</b> <<a href="mailto:evan@4-am.com">evan@4-am.com</a>> 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>> I propose to create a new type of scoping rule, which I will call<br>> "explicit" lexical scoping, that will co-exist with the current<br>> "implicit" scoping rule that exists in Python today.
<br><br>I'd like to toss one more variant into the mix. 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> scope as outer # "outer" is an arbitrary identifier<br> def inc():<br> outer.num += 1<br> return outer.num<br> 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> num = 1<br> def inc():<br> locals[1].num += 1<br> return outer.num<br> return inc<br><br><br>Where, for CPython, locals[n] gives access to NamespaceProxy(sys._getframe(n).f_locals). 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). 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. 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>