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

Carl Matthew Johnson cmjohnson.mailinglist at gmail.com
Tue Sep 27 08:34:39 CEST 2011


On Sep 26, 2011, at 7:05 PM, Greg Ewing wrote:

> For example, when someone writes
> 
>    for i in things:
>        def f():
>          dosomethingwith(i)
>        squirrelaway(f)
> 
> and get surprised by the result, he's effectively
> assuming that the for-loop creates a new binding for
> i each time around. He may not *realise* he's assuming
> that, but he is.

For the record, I remember being surprised when I learned that for doesn't create a new scope.

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...")

-- Carl Johnson 


More information about the Python-ideas mailing list