free variables /cell objects question

Duncan Booth duncan.booth at invalid.invalid
Tue Jan 23 07:11:31 EST 2007


"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