[Python-Dev] ctypes: is it intentional that id() is the only way to get the address of an object?

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Jan 18 19:35:14 EST 2019


Steven D'Aprano wrote:

> The sample code I've been shown is this:
> 
>     pointer_to_obj = id(obj)
>     from_deref = ctypes.cast(pointer_to_obj, ctypes.py_object).value
>     from_deref is obj  # True

There's no need to use id() or casting to create a ctypes.py_object
instance, you can just call it:

 >>> obj = (1,2,3)
 >>> obj
(1, 2, 3)
 >>> p = ctypes.py_object(obj)
 >>> p
py_object((1, 2, 3))
 >>> p.value
(1, 2, 3)
 >>> p.value is obj
True

-- 
Greg


More information about the Python-Dev mailing list