Cell objects and their values

Jeff Epler jepler at unpythonic.net
Wed Apr 21 19:59:54 EDT 2004


This seems to work, but it's undocumented (specifically, func_closure as
an argument to new.function is undocumented) and makes my tummy feel
funny when I think about it.

>>> import new
>>> def cell_get(cell):
...    def f(): return cell
...    return new.function(f.func_code, {}, "f", (), (cell,))()
...    

cell_get's 'f' does LOAD_DEREF 0 / RETURN, new.function() chooses the
created function's func_closure, which is the called right away.

>>> def f():
...    x = 3
...    def g():
...        return x
...    return g.func_closure[0]
...
>>> c = f()
<cell at 0x943b704: int object at 0x93690dc>
>>> cell_get(c)
3

... it works!

Jeff




More information about the Python-list mailing list