free variables /cell objects question
gangesmaster
tomerfiliba at gmail.com
Tue Jan 23 09:46:20 EST 2007
ugliness :)
so this is why [lambda: i for i in range(10)] will always return 9.
imho that's a bug, not a feature.
thanks.
-tomer
Duncan Booth wrote:
> "gangesmaster" <tomerfiliba at gmail.com> wrote:
>
> > what problem does the cell object solve?
>
> The closure represents the variable, not the object. So if x is rebound to
> a different object your inner function g() will now access the new object.
> If the object itself was passed to MAKE_CLOSURE then g would only ever see
> the value of x from the instant when g was defined.
>
> >>> def f(x):
> def g():
> print "x is", x
> g()
> x += 1
> g()
>
>
> >>> f(1)
> x is 1
> x is 2
More information about the Python-list
mailing list