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

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Sep 27 09:03:37 CEST 2011

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.


More information about the Python-ideas mailing list