free variables /cell objects question

Carl Banks pavlovevidence at
Wed Jan 24 05:25:42 CET 2007

On Jan 23, 9:46 am, "gangesmaster" <tomerfil... at> wrote:
> ugliness :)
> so this is why [lambda: i for i in range(10)] will always return 9.
> imho that's a bug, not a feature.

It's a feature.

If you were to write a nested function like this, where a, b, c, and d,
are cell variables:

    print a,b,c,d

then it wouldn't work as intended if you passed the variable itself to
the cell rather than a reference.  Nested functions have to support
this as well as to create lexical closures.

The thing is, newbies are very likely to use nested functions like I've
shown above, whereas few newbies are likely to use closures.  Python
has correctly, IMHO, chosen the way that minimizes surprises for

Carl Banks

More information about the Python-list mailing list