函数对象

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

PyFunctionObject

用于功能的 C 结构体。

PyTypeObject PyFunction_Type

这是一个 PyTypeObject 实例并表示 Python 函数类型。 它作为 types.FunctionType 向 Python 程序员公开。

int PyFunction_Check(PyObject *o)

如果 o 是函数对象(具有类型 PyFunction_Type ),则返回 true 。 参数不能为 NULL

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

返回与代码对象 code 关联的新函数对象。 globals 必须是一个字典,该函数可以访问全局变量。

从代码对象中检索函数的 docstring 和 name 。 __module__globals 中检索。 参数 defaults 、 annotations 和 closure 设置为*NULL* 。 __qualname__ 设置为与函数名称相同的值。

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

PyFunction_New() ,但也允许设置函数对象的 __qualname__ 属性。 qualname 应该是 unicode 对象或 NULL ;如果为 NULL ,则 __qualname__ 属性设置为与 __name__ 属性相同的值。

3.3 新版功能.

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

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

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

Return the globals dictionary associated with the function object op.

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

Return the __module__ attribute of the function object op. This is normally a string containing the module name, but can be set to any other object by Python code.

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

Return the argument default values of the function object op. This can be a tuple of arguments or NULL.

int PyFunction_SetDefaults(PyObject *op, PyObject *defaults)

Set the argument default values for the function object op. defaults must be Py_None or a tuple.

Raises SystemError and returns -1 on failure.

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

Return the closure associated with the function object op. This can be NULL or a tuple of cell objects.

int PyFunction_SetClosure(PyObject *op, PyObject *closure)

Set the closure associated with the function object op. closure must be Py_None or a tuple of cell objects.

Raises SystemError and returns -1 on failure.

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

Return the annotations of the function object op. This can be a mutable dictionary or NULL.

int PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)

Set the annotations for the function object op. annotations must be a dictionary or Py_None.

Raises SystemError and returns -1 on failure.