【问题标题】:Where can I view the code for the built-in Python method id() (Python 3.x)? [duplicate]在哪里可以查看内置 Python 方法 id() (Python 3.x) 的代码? [复制]
【发布时间】:2019-06-02 14:47:51
【问题描述】:

在哪里可以查看内置 Python 方法 id() (Python 3.x) 的代码?

我一直在 Python 的 GitHub 页面上搜索它,但没有任何运气。我查看了与此相关的其他问题,但找不到具体的方法 id()。

我想看看这里是否有人知道该方法的代码在哪里。

【问题讨论】:

标签: python python-3.x cpython python-internals


【解决方案1】:

与大多数内置名称一样,id() 函数在 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);
}

这使用Python C-API function PyLong_FromVoidPtr()将指针v引用的Python对象的地址转换为Pythonint对象(使用system-specific cast to a C unsigned long or unsigned long long integer first

【讨论】:

  • 谢谢!这很有帮助!我试图将此标记为答案,但它说我必须等待。我会尽快标记的。
猜你喜欢
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 1970-01-01
  • 2017-07-18
  • 2015-06-27
  • 2014-03-09
  • 1970-01-01
  • 2021-05-16
相关资源
最近更新 更多