函式物件 (Function Objects)

這有一些特用於 Python 函式的函式。

type PyFunctionObject

用於函式的 C 結構。

PyTypeObject PyFunction_Type

這是個 PyTypeObject 的實例,且代表了 Python 函式型別,Python 程式設計者可透過 types.FunctionType 使用它。

int PyFunction_Check(PyObject *o)

如果 o 是個函式物件(擁有 PyFunction_Type 的型別)則回傳 true。參數必須不為 NULL。此函式必能成功執行。

PyObject *PyFunction_New(PyObject *code, PyObject *globals)
回傳值:新的參照。

回傳一個與程式碼物件 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 code object's co_qualname field.

PyObject *PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
回傳值:新的參照。

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 the code object's co_qualname field.

在 3.3 版新加入.

PyObject *PyFunction_GetCode(PyObject *op)
回傳值:借用參照。

回傳與程式碼物件相關的函式物件 op

PyObject *PyFunction_GetGlobals(PyObject *op)
回傳值:借用參照。

回傳與全域函式字典相關的函式物件 op

PyObject *PyFunction_GetModule(PyObject *op)
回傳值:借用參照。

Return a borrowed reference to the __module__ attribute of the function object op. It can be NULL.

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)
回傳值:借用參照。

回傳函式物件 op 的引數預設值,這可以是一個含有多個引數的 tuple(元組)或 NULL

int PyFunction_SetDefaults(PyObject *op, PyObject *defaults)

設定函式物件 op 的引數預設值。defaults 必須是 Py_None 或一個 tuple。

引發 SystemError 且在失敗時回傳 -1

PyObject *PyFunction_GetClosure(PyObject *op)
回傳值:借用參照。

回傳與函式物件 op 相關聯的閉包,這可以是個 NULL 或是一個包含 cell 物件的 tuple。

int PyFunction_SetClosure(PyObject *op, PyObject *closure)

設定與函式物件 op 相關聯的閉包,closure 必須是 Py_None 或是一個包含 cell 物件的 tuple。

引發 SystemError 且在失敗時回傳 -1

PyObject *PyFunction_GetAnnotations(PyObject *op)
回傳值:借用參照。

回傳函式物件 op 的標註,這可以是一個可變動的 (mutable) 字典或 NULL

int PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)

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

引發 SystemError 且在失敗時回傳 -1