Search code examples
pythonpython-3.xcpythonpython-internals

Where can I view the code for the built-in Python method id() (Python 3.x)?


Where can I view the code for the built-in Python method id() (Python 3.x)?

I've been searching for it on Python's GitHub page, but am not having any luck. I've looked at other questions related to this, but could not find the specific method id().

Thought I'd see if anyone here knows where the code for that method is.


Solution

  • Like most built-in names, the id() function is defined in the Python/bltinmodule.c source file:

    static PyObject *
    builtin_id(PyModuleDef *self, PyObject *v)
    /*[clinic end generated code: output=0aa640785f697f65 input=5a534136419631f4]*/
    {
        return PyLong_FromVoidPtr(v);
    }
    

    This uses the Python C-API function PyLong_FromVoidPtr() to turn the address of the Python object referenced by the pointer v into a Python int object (using a system-specific cast to a C unsigned long or unsigned long long integer first)