Categories
Python Answers

How to get object by id() with Python?

Spread the love

Sometimes, we want to get object by id() with Python.

In this article, we’ll look at how to get object by id() with Python.

How to get object by id() with Python?

To get object by id() with Python, w ecan use the ctypes module.

For instance, we write

import ctypes

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

to get the object a‘s ID with id(a).

Then we get the object by the ID with

ctypes.cast(id(a), ctypes.py_object)

Finally, we get the object’s value with value.

Conclusion

To get object by id() with Python, w ecan use the ctypes module.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *