Accessing the contents of a 'cell' object from Python

P

paul cannon

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.

You can get at a cell object containing a given value by making a quick
closure and looking at the func_closure attribute:

(lambda x: lambda: x)(some_value).func_closure[0]

but there's not anything we can easily do with that object to find out
what it's pointing at. The str() representation helpfully tells us the
id of the contained value, but I'm not aware of a way to get at an
object given its id [1].

So I thought it might be useful to add such a method to the cell object
type. Attached is a sample patch that does so (against python2.4). Does
this look like the sort of thing that would be useful to anyone else?

How does one go about getting small changes like this into Python
anyway?

--
paul

[1] Yes, the id is the object's pointer in cPython, but even I don't
think it would be a good idea to allow accessing an object via the id,
even if it's possible. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top