[Python-ideas] For-loop variable scope: simultaneous possession and ingestion of cake
George Sakkis
george.sakkis at gmail.com
Mon Oct 13 02:24:19 CEST 2008
On Fri, Oct 3, 2008 at 10:56 PM, Greg Ewing <greg.ewing at canterbury.ac.nz>wrote:
> Guido van Rossum wrote:
>
> This leads me to reject claims that "the for-loop is broken" and in
>> particular clamoring for fixing the for-loop without allowing us to
>> fix this example.
>>
>
> Yeah, I never said the for-loop was the *only*
> thing that's broken. :-)
>
> Perhaps "broken" is too strong a word. What I really
> mean is that it's designed in a way that interacts
> badly with nested functions.
>
> More generally, Python's inability to distinguish
> clearly between creating new bindings and changing
> existing bindings interacts badly with nested
> functions.
>
> I agree that the wider problem needs to be addressed
> somehow, and perhaps that should be the starting
> point. Depending on the solution adopted, we can
> then look at whether a change to the for-loop is
> still needed.
Since this idea didn't get much steam, a more modest proposal would be to
relax the restriction on cells: allow the creation of new cells and the
rebinding of func_closure in pure Python. Then one could explicitly create a
new scope without any other change in the language through a 'localize'
decorator that would create a new cell for every free variable (i.e. global
or value from an enclosing scope) of the function:
lst = []
for i in range(10):
@localize
def f(): print i
lst.append(f)
lst.append(localize(lambda: i**2))
I'd love to be proven wrong but I don't think localize() can be implemented
in current Python.
George
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20081012/6b406016/attachment.html>
More information about the Python-ideas
mailing list