函式物件 (Function Objects)

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

typePyFunctionObject

用於函式的 C 結構。

PyTypeObjectPyFunction_Type

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

intPyFunction_Check(PyObject*o)

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

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

回傳一個與程式碼物件code 相關聯的函式物件。globals 必須是一個帶有函式能夠存取的全域變數的字典。

函式的文件字串 (docstring) 和名稱是從程式碼物件所取得,__module__ 是自globals 所取得。引數預設值、標註 (annotation) 和閉包 (closure) 被設為NULL__qualname__ 被設為和程式碼物件co_qualname 欄位相同的值。

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

PyFunction_New() 相似,但也允許函式物件__qualname__ 屬性的設定,qualname 應為一個 unicode 物件或是NULL;如為NULL__qualname__ 屬性會被設為與程式碼物件co_qualname 欄位相同的值。

在 3.3 版被加入.

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

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

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

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

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

回傳一個函式物件op__module__ 屬性的borrowed reference,它可以是NULL

這通常是個包含模組名稱的字串,但可以被 Python 程式設為任何其他物件。

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

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

intPyFunction_SetDefaults(PyObject*op,PyObject*defaults)

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

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

voidPyFunction_SetVectorcall(PyFunctionObject*func,vectorcallfuncvectorcall)

為一個給定的函式物件func 設定 vectorcall 欄位。

Warning: extensions using this API must preserve the behaviorof the unaltered (default) vectorcall function!

在 3.12 版被加入.

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

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

intPyFunction_SetClosure(PyObject*op,PyObject*closure)

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

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

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

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

intPyFunction_SetAnnotations(PyObject*op,PyObject*annotations)

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

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

intPyFunction_AddWatcher(PyFunction_WatchCallbackcallback)

Registercallback as a function watcher for the current interpreter.Return an ID which may be passed toPyFunction_ClearWatcher().In case of error (e.g. no more watcher IDs available),return-1 and set an exception.

在 3.12 版被加入.

intPyFunction_ClearWatcher(intwatcher_id)

Clear watcher identified bywatcher_id previously returned fromPyFunction_AddWatcher() for the current interpreter.Return0 on success, or-1 and set an exception on error(e.g. if the givenwatcher_id was never registered.)

在 3.12 版被加入.

typePyFunction_WatchEvent

Enumeration of possible function watcher events:

  • PyFunction_EVENT_CREATE

  • PyFunction_EVENT_DESTROY

  • PyFunction_EVENT_MODIFY_CODE

  • PyFunction_EVENT_MODIFY_DEFAULTS

  • PyFunction_EVENT_MODIFY_KWDEFAULTS

在 3.12 版被加入.

typedefint(*PyFunction_WatchCallback)(PyFunction_WatchEventevent,PyFunctionObject*func,PyObject*new_value)

Type of a function watcher callback function.

Ifevent isPyFunction_EVENT_CREATE orPyFunction_EVENT_DESTROYthennew_value will beNULL. Otherwise,new_value will hold aborrowed reference to the new value that is about to be stored infunc for the attribute that is being modified.

The callback may inspect but must not modifyfunc; doing so could haveunpredictable effects, including infinite recursion.

Ifevent isPyFunction_EVENT_CREATE, then the callback is invokedafterfunc has been fully initialized. Otherwise, the callback is invokedbefore the modification tofunc takes place, so the prior state offunccan be inspected. The runtime is permitted to optimize away the creation offunction objects when possible. In such cases no event will be emitted.Although this creates the possibility of an observable difference ofruntime behavior depending on optimization decisions, it does not changethe semantics of the Python code being executed.

Ifevent isPyFunction_EVENT_DESTROY, Taking a reference in thecallback to the about-to-be-destroyed function will resurrect it, preventingit from being freed at this time. When the resurrected object is destroyedlater, any watcher callbacks active at that time will be called again.

If the callback sets an exception, it must return-1; this exception willbe printed as an unraisable exception usingPyErr_WriteUnraisable().Otherwise it should return0.

There may already be a pending exception set on entry to the callback. Inthis case, the callback should return0 with the same exception stillset. This means the callback may not call any other API that can set anexception unless it saves and clears the exception state first, and restoresit before returning.

在 3.12 版被加入.