函式(Function)物件

這有一些少數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)

回傳函式物件 op 標註。此可以是一個可變動的字典或 NULL

int PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)

設定函數物件 op 的標註。annotations 必須是一個字典或 Py_None

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