这是一个实用程序函数,基于“ Tiran”在HophatAbc所引用的讨论中提出的评论,该评论将在Python2和3中都适用:
import _ctypesdef di(obj_id): """ Inverse of id() function. """ return _ctypes.PyObj_FromPtr(obj_id)if __name__ == '__main__': a = 42 b = 'answer' print(di(id(a))) # -> 42 print(di(id(b))) # -> answer



