Get object by id()? [duplicate]

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

output:

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.

Leave a Comment