Accessing Objects Based On Their ID

T

Tim Daneliuk

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,
 
D

Diez B. Roggisch

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

Fredrik Lundh

Tim said:
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>
 
T

Tim Daneliuk

Diez said:
No.




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

Terry Reedy

Tim Daneliuk said:
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
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top