Created
January 27, 2020 08:14
-
-
Save y56/a3ce0dda37d52cd19cb43461e2ca1cab to your computer and use it in GitHub Desktop.
Python inverse function of id(…) built-in function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://stackoverflow.com/questions/24815771/python-inverse-function-of-id-built-in-function | |
================================ | |
Is there a reverse or inverse of the id built-in function? | |
I was thinking of using it to encode and decode string without taking too much time or having a lot of overhead like the PyCrypto library. | |
The need for me is quite simple so I don't want to use PyCrypto for a simple encode and decode. | |
Something like: | |
>>> id("foobar") | |
4330174256 | |
>>> reverse_id(4330174256) # some function like this to reverse. | |
"foobar" | |
================================ | |
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 | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment