Accessing Objects Based On Their ID
Fredrik Lundh
fredrik at pythonware.com
Tue Feb 15 06:45:19 EST 2005
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>
More information about the Python-list
mailing list