[Python-Dev] PEP for Better Control of Nested Lexical Scopes (original) (raw)

Almann T. Goo almann.goo at gmail.com
Tue Feb 21 17:15:40 CET 2006


Why not just use a class?

def incgen(start=0, inc=1) : class incrementer(object): a = start - inc def call(self): self.a += inc return self.a return incrementer() a = incgen(7, 5) for n in range(10): print a(),

Because I think that this is a workaround for a concept that the language doesn't support elegantly with its lexically nested scopes.

IMO, you are emulating name rebinding in a closure by creating an object to encapsulate the name you want to rebind--you don't need this workaround if you only need to access free variables in an enclosing scope. I provided a "lighter" example that didn't need a callable object but could use any mutable such as a list.

This kind of workaround is needed as soon as you want to re-bind a parent scope's name, except in the case when the parent scope is the global scope (since there is the "global" keyword to handle this). It's this dichotomy that concerns me, since it seems to be against the elegance of Python--at least in my opinion.

It seems artificially limiting that enclosing scope name rebinds are not provided for by the language especially since the behavior with the global scope is not so. In a nutshell I am proposing a solution to make nested lexical scopes to be orthogonal with the global scope and removing a "wart," as Jeremy put it, in the language.

-Almann

-- Almann T. Goo almann.goo at gmail.com



More information about the Python-Dev mailing list