osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


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