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.