Accessing the contents of a 'cell' object from Python
paul cannon
paul-pythonlist at nafpik.com
Tue Mar 15 19:12:11 EST 2005
On Tue, Mar 15, 2005 at 03:08:19PM -0700, paul cannon wrote:
> Having poked around a little bit, I found there doesn't appear to be any
> way to get at the contents of a cell object from Python. It's not the
> sort of thing that one needs to be doing very frequently, but I've run
> into a few situations recently where it would be really useful from a
> debugging standpoint.
Okay, I did come up with one solution- create a new function that just
returns a value from its own closure, and manufacture its closure from
the cell you already have.
So..
import new
def get_cell_value(cell):
return new.function(
(lambda x: lambda: x)(0).func_code, {}, None, None, (cell,)
)()
It could be optimized a bit by precalculating the code object.
I do still think the earlier solution (giving the cell objects a method)
is cleaner and better long-term, but this will do.
--
paul
More information about the Python-list
mailing list