Function Objects

There are a few functions specific to Python functions.

PyFunctionObject

The C structure used for functions.

PyTypeObjectPyFunction_Type

This is an instance ofPyTypeObject and represents the Python functiontype. It is exposed to Python programmers astypes.FunctionType.

intPyFunction_Check(PyObject *o)

Return true ifo is a function object (has typePyFunction_Type).The parameter must not beNULL.

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

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

The function’s docstring and name are retrieved from the code object.__module__is retrieved fromglobals. The argument defaults, annotations and closure areset toNULL.__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.

AsPyFunction_New(), but also allows setting the function object’s__qualname__ attribute.qualname should be a unicode object orNULL;ifNULL, the__qualname__ attribute is set to the same value as its__name__ attribute.

New in version 3.3.

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

Return the code object associated with the function objectop.

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

Return the globals dictionary associated with the function objectop.

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

Return the__module__ attribute of the function objectop. This is normallya string containing the module name, but can be set to any other object byPython code.

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

Return the argument default values of the function objectop. This can be atuple of arguments orNULL.

intPyFunction_SetDefaults(PyObject *op,PyObject *defaults)

Set the argument default values for the function objectop.defaults must bePy_None or a tuple.

RaisesSystemError and returns-1 on failure.

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

Return the closure associated with the function objectop. This can beNULLor a tuple of cell objects.

intPyFunction_SetClosure(PyObject *op,PyObject *closure)

Set the closure associated with the function objectop.closure must bePy_None or a tuple of cell objects.

RaisesSystemError and returns-1 on failure.

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

Return the annotations of the function objectop. This can be amutable dictionary orNULL.

intPyFunction_SetAnnotations(PyObject *op,PyObject *annotations)

Set the annotations for the function objectop.annotationsmust be a dictionary orPy_None.

RaisesSystemError and returns-1 on failure.