(original) (raw)
On 22-Feb-06, at 9:28 PM, python-dev-request@python.org wrote:
On 21-Feb-06, at 11:21 AM,�Almann T. Goo" <almann.goo@gmail.com>�wrote: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 thelanguage doesn't support elegantly with its lexically nested scopes.IMO, you are emulating name rebinding in a closure by creating anobject to encapsulate the name you want to rebind--you don't need thisworkaround if you only need to access free variables in an enclosingscope.� I provided a "lighter" example that didn't need a callableobject but could use any mutable such as a list.This kind of workaround is needed as soon as you want to re-bind aparent scope's name, except in the case when the parent scope is theglobal scope (since there is the "global" keyword to handle this).�It's this dichotomy that concerns me, since it seems to be against theelegance of Python--at least in my opinion.It seems artificially limiting that enclosing scope name rebinds arenot provided for by the language especially since the behavior withthe global scope is not so.� In a nutshell I am proposing a solutionto make nested lexical scopes to be orthogonal with the global scopeand removing a "wart," as Jeremy put it, in the language.-Almann--Almann T. GooIf 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:��http://www.thescripts.com/forum/thread43418.html)� -Brendan--Brendan Simons
Sorry, this got hung up in my email outbox.� I see the thread has touched on this idea in the meantime.� So, yeah.� Go team.
� Brendan
--
Brendan Simons