函数对象

有一些特定于Python函数的函数。

type PyFunctionObject

用于函数的C结构。

PyTypeObject PyFunction_Type

这是一个 PyTypeObject 并表示python函数类型。它向Python程序员公开如下 types.FunctionType .

int PyFunction_Check(PyObject *o)

如果满足以下条件,则返回TRUE o 是函数对象(具有类型 PyFunction_Type )。该参数不得为 NULL 。此功能总是成功的。

PyObject *PyFunction_New(PyObject *code, PyObject *globals)
Return value: New reference.

返回与代码对象关联的新函数对象 code . globals 必须是具有函数可访问的全局变量的字典。

从代码对象中检索函数的docstring和名称。 __module__ 检索自 全局变量 . 参数默认值、注释和闭包设置为 NULL . __qualname__ 设置为与函数名相同的值。

PyObject *PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
Return value: New reference.

AS PyFunction_New() ,但也允许设置函数对象的 __qualname__ 属性。 质量名称 应该是unicode对象或 NULL 如果 NULL , the __qualname__ 属性设置为与其相同的值 __name__ 属性。

3.3 新版功能.

PyObject *PyFunction_GetCode(PyObject *op)
Return value: Borrowed reference.

返回与函数对象关联的代码对象 op .

PyObject *PyFunction_GetGlobals(PyObject *op)
Return value: Borrowed reference.

返回与函数对象关联的全局字典 op .

PyObject *PyFunction_GetModule(PyObject *op)
Return value: Borrowed reference.

返回 __module__ 函数对象的属性 op . 这通常是一个包含模块名的字符串,但可以通过Python代码设置为任何其他对象。

PyObject *PyFunction_GetDefaults(PyObject *op)
Return value: Borrowed reference.

返回函数对象的参数默认值 op . 这可以是一组参数,或者 NULL .

int PyFunction_SetDefaults(PyObject *op, PyObject *defaults)

设置函数对象的参数默认值 op . 默认值 必须是 Py_None 或元组。

引发 SystemError 回报 -1 失败论。

PyObject *PyFunction_GetClosure(PyObject *op)
Return value: Borrowed reference.

返回与函数对象关联的闭包 op . 这可以 NULL 或者一组单元格对象。

int PyFunction_SetClosure(PyObject *op, PyObject *closure)

设置与函数对象关联的闭包 op . 关闭 必须是 Py_None 或者一组单元格对象。

引发 SystemError 回报 -1 失败论。

PyObject *PyFunction_GetAnnotations(PyObject *op)
Return value: Borrowed reference.

返回函数对象的注释 op . 这可能是一本可变字典,或者 NULL .

int PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)

设置函数对象的注释 op . 注解 必须是字典或 Py_None .

引发 SystemError 回报 -1 失败论。