Oct-24-2019, 04:16 PM
(This post was last modified: Oct-24-2019, 04:17 PM by Gribouillis.)
pyThon_ Wrote:What makes you think the unique identifier is the memory address?
This is CPython's definition of the id() function (this is C code)
static PyObject * builtin_id(PyModuleDef *self, PyObject *v) /*[clinic end generated code: output=0aa640785f697f65 input=5a534136419631f4]*/ { PyObject *id = PyLong_FromVoidPtr(v); if (id && PySys_Audit("builtins.id", "O", id) < 0) { Py_DECREF(id); return NULL; } return id; }whereas PyLong_FromVoidPtr()'s definition is
PyObject * PyLong_FromVoidPtr(void *p) { #if SIZEOF_VOID_P <= SIZEOF_LONG return PyLong_FromUnsignedLong((unsigned long)(uintptr_t)p); #else #if SIZEOF_LONG_LONG < SIZEOF_VOID_P # error "PyLong_FromVoidPtr: sizeof(long long) < sizeof(void*)" #endif return PyLong_FromUnsignedLongLong((unsigned long long)(uintptr_t)p); #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ }It means that CPython casts the object's address to unsigned long or unsigned long long, then casts this value to a python int object to build the identifier.
That the object's ID is related to the memory address is simply not guaranteed by the specifications of the python language and it can be implemented differently.