free variables /cell objects question

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


On Jan 23, 9:46 am, "gangesmaster" <tomerfil... at gmail.com> 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_values():
    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
newbies.


Carl Banks




More information about the Python-list mailing list