[Python-Dev] PEP for Better Control of Nested Lexical Scopes

Brendan Simons spam4bsimons at yahoo.ca
Thu Feb 23 00:45:18 CET 2006

On 21-Feb-06, at 11:21 AM, Almann T. Goo" <almann.goo at gmail.com>

>> 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

If I may be so bold, couldn't this be addressed by introducing a  
"rebinding" operator?  So the ' = ' operator would continue to create  
a new name in the current scope, and the (say) ' := ' operator would  
for an existing name to rebind.   The two operators would highlight  
the special way Python handles variable / name assignment, which many  
newbies miss.

(from someone who was surprised by this quirk of Python before:   

Brendan Simons

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20060222/94282906/attachment.html 

More information about the Python-Dev mailing list