Let's say I have an id of a Python object, which I retrieved by doing id(thing). How do I find thing again by the id number I was given?

  • I'm curious: why do you want to do this? What is your objective? – Craig McQueen Sep 9 '09 at 12:35
  • 1
    @Craig McQueen: stackoverflow.com/questions/1400295/… – Ram Rachum Sep 10 '09 at 18:06
  • I couldn't re-find the source of this but I thought that what is returned by id() is whatever the particular distribution wants it to be. Restated CPython may, at this time, return a memory-like address now, but other distributions could return different object types or ints that are not memory pointers. It would be nice if there was a built in function to obtain an object by what is returned by id(). Although other then persistence use-cases are hard to imagine. Also although *variable, like C seems to make sense; I love Python for it's lack of punctuation trickery like most other languages. – DevPlayer Jun 24 '15 at 2:34
  • Suggest you see answer to duplicate question Is it possible to dereference variable id's? – martineau Mar 11 '17 at 18:35
  • 1
    Legitimate reason to do this: debugging. The default repr on objects includes their memory address. Sometimes when debugging (especially interactively), you want to be able to access that object without trying to dig into where it is defined. – asmeurer Sep 22 '17 at 21:40

You'll probably want to consider implementing it another way. Are you aware of the weakref module?

(Edited) The Python weakref module lets you keep references, dictionary references, and proxies to objects without having those references count in the reference counter. They're like symbolic links.

  • 5
    Sometimes you can't create weak reference to the object, e.g: TypeError: cannot create weak reference to 'lxml.etree._Element' object – darkk Jun 23 '11 at 9:17

If the object is still there, this can be done by ctypes:

import ctypes
a = "hello world"
print ctypes.cast(id(a), ctypes.py_object).value


hello world

If you don't know whether the object is still there, this is a recipe for undefined behavior and weird crashes or worse, so be careful.

  • 13
    In CPython, today, anyhow. :^) – DSM Mar 29 '13 at 14:24
  • 5
    This one is a perfect answer! – Hamid FzM Nov 11 '13 at 21:44
  • 9
    @HamidFzM No, not really. If I have an ID, I maybe don't even know whether the object still exists or not. – glglgl Jul 21 '14 at 8:13
  • 2
    repr outputs the hexadecimal representation of id(a), in order to use ctypes one must convert it back to decimal by using int(hexid, 0). just my two cents here – architectonic Mar 18 '16 at 9:07
  • Is there a way to check if a memory address exists first before doing this? If you pass an invalid value (say, because it's been garbage collected), the interpreter segfaults. – asmeurer Sep 22 '17 at 21:30

You can use the gc module to get all the objects currently tracked by the Python garbage collector.

import gc

def objects_by_id(id_):
    for obj in gc.get_objects():
        if id(obj) == id_:
            return obj
    raise Exception("No found")
  • 7
    This has an aliasing issue: an ID obtained at an arbitrary point in the past may now refer to a different object. – chaos Sep 8 '09 at 22:37
  • 8
    As long as you've maintained a reference to the object, that won't happen. Just the same, this is generally a bad idea. – Glenn Maynard Sep 8 '09 at 23:16
  • I agree with many of the other commenters: don't do this. Make your own dictionary of objects. – Ned Batchelder Sep 9 '09 at 11:26

Short answer, you can't.

Long answer, you can maintain a dict for mapping IDs to objects, or look the ID up by exhaustive search of gc.get_objects(), but this will create one of two problems: either the dict's reference will keep the object alive and prevent GC, or (if it's a WeakValue dict or you use gc.get_objects()) the ID may be deallocated and reused for a completely different object.

Basically, if you're trying to do this, you probably need to do something differently.

  • I've been intending to throw away the id reference in the object's __del__, do you think that this will make sure things will not break? – Ram Rachum Sep 8 '09 at 22:44
  • 6
    +1: Agree: Don't do this. Simply create a proper dictionary of objects with proper keys -- you'll be a lot happier. – S.Lott Sep 8 '09 at 23:51
  • 1
    @cool-RR: Yeah, you should be safe with that. – chaos Sep 9 '09 at 12:57

Just mentioning this module for completeness. This code by Bill Bumgarner includes a C extension to do what you want without looping throughout every object in existence.

The code for the function is quite straightforward. Every Python object is represented in C by a pointer to a PyObject struct. Because id(x) is just the memory address of this struct, we can retrieve the Python object just by treating x as a pointer to a PyObject, then calling Py_INCREF to tell the garbage collector that we're creating a new reference to the object.

static PyObject *
di_di(PyObject *self, PyObject *args)
    PyObject *obj;
    if (!PyArg_ParseTuple(args, "l:di", &obj))
        return  NULL;

    return obj;

If the original object no longer exists then the result is undefined. It may crash, but it could also return a reference to a new object that's taken the location of the old one in memory.


eGenix mxTools library does provide such a function, although marked as "expert-only": mx.Tools.makeref(id)


This will do:

a = 0
id_a = id(a)
variables = {**locals(), **globals()}
for var in variables:
    if var_id == id_a:

But I suggest implementing a more decent way.

Your Answer

