函数对象

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

PyFunctionObject

用于函数的 C 结构体。

PyTypeObject PyFunction_Type

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

int PyFunction_Check(PyObject *o)

Return true if o is a function object (has type PyFunction_Type). The parameter must not be NULL.

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

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

The function’s docstring and name are retrieved from the code object. __module__ is retrieved from globals. The argument defaults, annotations and closure are set to NULL. __qualname__ is set to the same value as the function’s name.

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

As PyFunction_New(), but also allows setting the function object’s __qualname__ attribute. qualname should be a unicode object or NULL; if NULL, the __qualname__ attribute is set to the same value as its __name__ attribute.

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.

返回函数对象 op__module__ 属性,通常为一个包含了模块名称的字符串,但可以通过 Python 代码设为返回其他任意对象。

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.

失败时引发 SystemError 异常并返回 -1

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.

失败时引发 SystemError 异常并返回 -1

PyObject *PyFunction_GetAnnotations(PyObject *op)

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.

失败时引发 SystemError 异常并返回 -1