Accessing Objects Based On Their ID

Discussion in 'Python' started by Tim Daneliuk, Feb 15, 2005.

  1. Tim Daneliuk

    Tim Daneliuk Guest

    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
    PGP Key: http://www.tundraware.com/PGP/
     
    Tim Daneliuk, Feb 15, 2005
    #1
    1. Advertising


  2. > 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
     
    Diez B. Roggisch, Feb 15, 2005
    #2
    1. Advertising

  3. 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, Feb 15, 2005
    #3
  4. > 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>
     
    Fredrik Lundh, Feb 15, 2005
    #4
  5. Tim Daneliuk

    Tim Daneliuk Guest

    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
    PGP Key: http://www.tundraware.com/PGP/
     
    Tim Daneliuk, Feb 15, 2005
    #5
  6. Tim Daneliuk

    Terry Reedy Guest

    "Tim Daneliuk" <> wrote in message
    news:...
    > 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
     
    Terry Reedy, Feb 15, 2005
    #6
    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. OguzhanF
    Replies:
    0
    Views:
    396
    OguzhanF
    Aug 5, 2003
  2. Peter Flynn
    Replies:
    0
    Views:
    394
    Peter Flynn
    Apr 11, 2004
  3. qazmlp
    Replies:
    2
    Views:
    423
  4. PicURLPy
    Replies:
    3
    Views:
    1,239
    David Coffin
    Dec 4, 2006
  5. Replies:
    2
    Views:
    78
    Stephen Chalmers
    Oct 6, 2005
Loading...

Share This Page