By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,548 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

Accessing the contents of a 'cell' object from Python

P: n/a
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


[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. :)

Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.