Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do objects work?
#7
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.
Reply


Messages In This Thread
How do objects work? - by pyThon_ - Oct-24-2019, 08:56 AM
RE: How do objects work? - by Gribouillis - Oct-24-2019, 09:35 AM
RE: How do objects work? - by jefsummers - Oct-24-2019, 02:01 PM
RE: How do objects work? - by pyThon_ - Oct-24-2019, 03:06 PM
RE: How do objects work? - by snippsat - Oct-24-2019, 03:41 PM
RE: How do objects work? - by pyThon_ - Oct-24-2019, 03:54 PM
RE: How do objects work? - by Gribouillis - Oct-24-2019, 04:16 PM
RE: How do objects work? - by pyThon_ - Oct-24-2019, 06:52 PM
RE: How do objects work? - by snippsat - Oct-24-2019, 04:34 PM
RE: How do objects work? - by Gribouillis - Oct-24-2019, 10:05 PM
RE: How do objects work? - by pyThon_ - Oct-24-2019, 11:13 PM
RE: How do objects work? - by Gribouillis - Oct-25-2019, 06:13 AM
RE: How do objects work? - by jefsummers - Oct-25-2019, 03:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do integer objects work? pyThon_ 2 2,002 Oct-22-2019, 01:36 AM
Last Post: wavic
  Python - Make Json objects to work concurrently through Threads? WeInThis 0 2,708 Sep-22-2017, 11:31 AM
Last Post: WeInThis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020