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

Steven D'Aprano steve at pearwood.info
Thu Jan 17 05:26:06 EST 2019


Disclaimer: I'm not a ctypes expert, so I might have this completely 
wrong. If so, I apologise for the noise.

The id() function is documented as returning an abstract ID number. In 
CPython, that happens to have been implemented as the address of the 
object.

I understand that the only way to pass the address of an object to 
ctypes is to use that id. Is that intentional?

As I see it, there is a conflict between two facts:

- that id() returns a memory address is an implementation detail; as 
such users should not rely on it, as the implementation could (in 
principle) change without notice;

- but users using ctypes have no choice but to rely on id() returning 
the object memory address, as of it were an offical part of the API.

Implementations like PyPy which emulate ctypes, while objects don't have 
fixed memory locations, will surely have a problem here. I don't know 
how PyPy solves this.

Have I misunderstood something here?



-- 
Steve


More information about the Python-Dev mailing list