[Python-ideas] Tweaking closures and lexical scoping to include the function being defined

Guido van Rossum guido at python.org
Tue Sep 27 19:15:43 CEST 2011

On Tue, Sep 27, 2011 at 12:03 AM, Greg Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> Carl Matthew Johnson wrote:
>> This seems like a kind of Python 4000 feature though…
>> At the very least you'd need to preserve the ability to use
>> "old-style" for-loops by writing something like,
>> i = None
>> for i in things:
>>    ...
>> if i is not None: print("Processed one or more things...")
> There's actually a way of implementing this so that the
> above code continues to work. If the loop variable is
> referenced by a nested function, it will be in a cell.
> All you need to do is create a *new* cell each time
> round the loop instead of replacing the contents of the
> existing cell. Code after the loop will then see
> whatever was last assigned to the loop variable, as
> it does currently.

Greg, can you start a separate thread for this? It seems to be a much
bigger change to the language, and it deserves its own thread, not
mixed in with Nick's idea for nonlocal (even if the mechanism for both
will end up using cells :-).

(And yes, agreed that that can work.)

--Guido van Rossum (python.org/~guido)

More information about the Python-ideas mailing list