Accessing the contents of a 'cell' object from Python

Discussion in 'Python' started by paul cannon, Mar 15, 2005.

  1. paul cannon

    paul cannon Guest

    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. :)
     
    paul cannon, Mar 15, 2005
    #1
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.