函數(shù)對象?

有一些特定于 Python 函數(shù)的函數(shù)。

PyFunctionObject?

用于函數(shù)的 C 結(jié)構(gòu)體。

PyTypeObject PyFunction_Type?

這是一個 PyTypeObject 實例并表示 Python 函數(shù)類型。 它作為 types.FunctionType 向 Python 程序員公開。

int PyFunction_Check(PyObject?*o)?

如果 o 是函數(shù)對象 (類型為 PyFunction_Type) 則返回真值。 形參必須不為 NULL

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

返回與代碼對象 code 關(guān)聯(lián)的新函數(shù)對象。 globals 必須是一個字典,該函數(shù)可以訪問全局變量。

從代碼對象中提取函數(shù)的文檔字符串和名稱。 __module__ 會從 globals 中提取。 參數(shù) defaults, annotations 和 closure 設(shè)為 NULL。 __qualname__ 設(shè)為與函數(shù)名稱相同的值。

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

類似 PyFunction_New(),但還允許設(shè)置函數(shù)對象的 __qualname__ 屬性。 qualname 應(yīng)當(dāng)是 unicode 對象或 NULL;如果是 NULL__qualname__ 屬性設(shè)為與其 __name__ 屬性相同的值。

3.3 新版功能.

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

返回與函數(shù)對象 op 關(guān)聯(lián)的代碼對象。

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

返回與函數(shù)對象*op*相關(guān)聯(lián)的全局字典。

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

返回函數(shù)對象 op__module__ 屬性,通常為一個包含了模塊名稱的字符串,但可以通過 Python 代碼設(shè)為返回其他任意對象。

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

返回函數(shù)對象 op 的參數(shù)默認(rèn)值。 這可以是一個參數(shù)元組或 NULL。

int PyFunction_SetDefaults(PyObject?*op, PyObject?*defaults)?

為函數(shù)對象 op 設(shè)置參數(shù)默認(rèn)值。 defaults 必須為 Py_None 或一個元組。

失敗時引發(fā) SystemError 異常并返回 -1

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

返回關(guān)聯(lián)到函數(shù)對象 op 的閉包。 這可以是 NULL 或 cell 對象的元組。

int PyFunction_SetClosure(PyObject?*op, PyObject?*closure)?

設(shè)置關(guān)聯(lián)到函數(shù)對象 op 的閉包。 closure 必須為 Py_None 或 cell 對象的元組。

失敗時引發(fā) SystemError 異常并返回 -1 。

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

返回函數(shù)對象 op 的標(biāo)注。 這可以是一個可變字典或 NULL。

int PyFunction_SetAnnotations(PyObject?*op, PyObject?*annotations)?

設(shè)置函數(shù)對象 op 的標(biāo)注。 annotations 必須為一個字典或 Py_None。

失敗時引發(fā) SystemError 異常并返回 -1