1

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user1165664
  • 185
  • 1
  • 1
  • 8

1 Answers1

2

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)

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank you! This was very helpful! I'm trying to mark this as the answer, but it says I have to wait. I will mark it asap. – user1165664 Jan 07 '19 at 22:43