Function Objects¶
There are a few functions specific to Python functions.
PyFunctionObject¶The C structure used for functions.
- PyTypeObject
PyFunction_Type¶ This is an instance of
PyTypeObjectand represents the Python functiontype. It is exposed to Python programmers astypes.FunctionType.
- int
PyFunction_Check(PyObject *o)¶ Return true ifo is a function object (has type
PyFunction_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 to
NULL.__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.
As
PyFunction_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 or
NULL.
- int
PyFunction_SetDefaults(PyObject *op,PyObject *defaults)¶ Set the argument default values for the function objectop.defaults must be
Py_Noneor a tuple.Raises
SystemErrorand returns-1on failure.
- PyObject*
PyFunction_GetClosure(PyObject *op)¶ - Return value: Borrowed reference.
Return the closure associated with the function objectop. This can be
NULLor a tuple of cell objects.
- int
PyFunction_SetClosure(PyObject *op,PyObject *closure)¶ Set the closure associated with the function objectop.closure must be
Py_Noneor a tuple of cell objects.Raises
SystemErrorand returns-1on failure.
- PyObject *
PyFunction_GetAnnotations(PyObject *op)¶ - Return value: Borrowed reference.
Return the annotations of the function objectop. This can be amutable dictionary or
NULL.
- int
PyFunction_SetAnnotations(PyObject *op,PyObject *annotations)¶ Set the annotations for the function objectop.annotationsmust be a dictionary or
Py_None.Raises
SystemErrorand returns-1on failure.