FAQ
This is probably obvious to you Python Geniuses (tm) out there but,
it is very late and I am experiencing Brain Fade:

Given the ID of an object, is there a way to access it? For example,
if we have the ID of a class instance, is there a way to invoke its
methods and attributes knowning only that ID? Similarly, if we have the
ID of a function, is there a way to call it?

This comes up because of an implementation I had in mind wherein I
would store the IDs of a notionally linked-list of objects - but without
the link - I just want to store their IDs in the desired order. But later,
when I want to actually use the objects I need a way to get from ID back
to something accessible in the namespace...

TIA,
--
----------------------------------------------------------------------------
Tim Daneliuk tundra at tundraware.com
PGP Key: http://www.tundraware.com/PGP/

Search Discussions

  • Diez B. Roggisch at Feb 15, 2005 at 11:43 am

    Given the ID of an object, is there a way to access it? For example,
    if we have the ID of a class instance, is there a way to invoke its
    methods and attributes knowning only that ID? Similarly, if we have the
    ID of a function, is there a way to call it? No.
    This comes up because of an implementation I had in mind wherein I
    would store the IDs of a notionally linked-list of objects - but without
    the link - I just want to store their IDs in the desired order. But
    later, when I want to actually use the objects I need a way to get from ID
    back to something accessible in the namespace...
    Why only the id? A list only stores a reference to the object anyway - no
    copy of it. So you don't gain anything by using the id.
    --
    Regards,

    Diez B. Roggisch
  • Tim Daneliuk at Feb 15, 2005 at 7:28 pm

    Diez B. Roggisch wrote:

    Given the ID of an object, is there a way to access it? For example,
    if we have the ID of a class instance, is there a way to invoke its
    methods and attributes knowning only that ID? Similarly, if we have the
    ID of a function, is there a way to call it?

    No.

    This comes up because of an implementation I had in mind wherein I
    would store the IDs of a notionally linked-list of objects - but without
    the link - I just want to store their IDs in the desired order. But
    later, when I want to actually use the objects I need a way to get from ID
    back to something accessible in the namespace...

    Why only the id? A list only stores a reference to the object anyway - no
    copy of it. So you don't gain anything by using the id.
    Point taken... thanks.

    --
    ----------------------------------------------------------------------------
    Tim Daneliuk tundra at tundraware.com
    PGP Key: http://www.tundraware.com/PGP/
  • Fredrik Lundh at Feb 15, 2005 at 11:45 am

    Tim Daneliuk wrote:

    This is probably obvious to you Python Geniuses (tm) out there but,
    it is very late and I am experiencing Brain Fade:

    Given the ID of an object, is there a way to access it?
    short answer: no.

    longer answer: write a small C extension that casts an integer (or long integer)
    argument to a PyObject, increments the refcount, and returns the object. or
    use gc.get_objects() to get a list of all GC-aware objects, and see if your object
    is in there. etc. all solutions are fragile, non-portable, and/or inefficient.
    This comes up because of an implementation I had in mind wherein I
    would store the IDs of a notionally linked-list of objects - but without
    the link - I just want to store their IDs in the desired order. But later,
    when I want to actually use the objects I need a way to get from ID back
    to something accessible in the namespace...
    sounds like "import weakref" might be what you need.

    </F>
  • Fredrik Lundh at Feb 15, 2005 at 11:48 am

    longer answer: write a small C extension that casts an integer (or long integer)
    argument to a PyObject, increments the refcount, and returns the object.
    here's a pointer to a pointer to such a function, btw:

    http://mail.python.org/pipermail/python-list/1999-October/013715.html

    (see other messages in that thread for more on why this is a stupid idea)

    </F>
  • Terry Reedy at Feb 15, 2005 at 10:33 pm
    "Tim Daneliuk" <tundra at tundraware.com> wrote in message
    news:uc39e2-63h1.ln1 at eskimo.tundraware.com...
    Given the ID of an object, is there a way to access it?
    You got the right short answer already.
    Another long answer 'yes' is to keep a dictionary mapping ids to objects,
    but as Diez pointed out, you need a real reason to do this.

    In some respects, id() is like a siren (in the mythological sense) that
    Python would be better off without.

    Terry J. Reedy

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 15, '05 at 11:33a
activeFeb 15, '05 at 10:33p
posts6
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase