Python inverse function of id(…) built-in function

倾然丶 夕夏残阳落幕 提交于 2019-12-03 14:48:00
Victor Castillo Torres

I do not wanna to steal the credits from the man who answered the question

This can be done easily by ctypes:

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

hello world
dano

I think the base64 module would fit your needs here, rather than trying to use id:

>>> import base64
>>> base64.b64encode("foobar")
'Zm9vYmFy'
>>> base64.b64decode('Zm9vYmFy')
'foobar'

The answer to your literal question (can I look up an object by its id?) is answered here. The short answer is no, you can't. Edit: Victor Castillo Torres ponits out that this actually is possible if you're using CPython, via the ctypes module.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!