函式(Function)物件

這有一些少數Python函數的於具體說明。

PyFunctionObject

The C structure used for functions.

PyTypeObject PyFunction_Type

This is an instance of PyTypeObject and represents the Python function type. It is exposed to Python programmers as types.FunctionType.

int PyFunction_Check(PyObject *o)

如果 o 是函数对象 (类型为 PyFunction_Type) 则返回真值。 形参必须不为 NULL

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

Return a new function object associated with the code object code. globals must be a dictionary with the global variables accessible to the function.

从代码对象中提取函数的文档字符串和名称。 __module__ 会从 globals 中提取。 参数 defaults, annotations 和 closure 设为 NULL__qualname__ 设为与函数名称相同的值。

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

类似 PyFunction_New(),但还允许设置函数对象的 __qualname__ 属性。 qualname 应当是 unicode 对象或 NULL;如果是 NULL__qualname__ 属性设为与其 __name__ 属性相同的值。

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.

Return the __module__ attribute of the function object op. 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)
Return value: Borrowed reference.

返回函数对象 op 的参数默认值。 这可以是一个参数元组或 NULL

int PyFunction_SetDefaults(PyObject *op, PyObject *defaults)

为函数对象 op 设置参数默认值。 defaults 必须为 Py_None 或一个元组。

Raises SystemError and returns -1 on failure.

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

返回关联到函数对象 op 的闭包。 这可以是 NULL 或 cell 对象的元组。

int PyFunction_SetClosure(PyObject *op, PyObject *closure)

设置关联到函数对象 op 的闭包。 closure 必须为 Py_None 或 cell 对象的元组。

Raises SystemError and returns -1 on failure.

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

返回函数对象 op 的标注。 这可以是一个可变字典或 NULL

int PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)

设置函数对象 op 的标注。 annotations 必须为一个字典或 Py_None

Raises SystemError and returns -1 on failure.