import struct _builtin_id = id _ptr_max = 256 ** struct.calcsize("P") class id(long): """Replacement for the built-in id() function. Instances of 'id' keep a reference to the original object, to prevent the memory address from becoming meaningless. The object is available in the public 'obj' attribute. Additionally, this id() never produces negative numbers. """ def __new__(cls, obj): my_id = _builtin_id(obj) % _ptr_max result = super(id, cls).__new__(cls, my_id) result.obj = obj return result def __repr__(self): return "" % (self, type(self.obj).__name__)