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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don Adams
    Replies:
    1
    Views:
    581
    Martin Honnen
    Mar 5, 2004
  2. paul cannon
    Replies:
    0
    Views:
    412
    paul cannon
    Mar 16, 2005
  3. Jeff Epler
    Replies:
    1
    Views:
    329
    Duncan Booth
    Mar 16, 2005
  4. John Doe

    accessing object contents

    John Doe, Mar 19, 2011, in forum: Ruby
    Replies:
    2
    Views:
    79
    Brian Candler
    Mar 19, 2011
  5. Justin C
    Replies:
    4
    Views:
    165
    Justin C
    Jul 10, 2006
Loading...

Share This Page