blob: ad008425f381165dd5e5e49d2a6971839a4fa629 [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
3.. _function-objects:
4
5Function Objects
6----------------
7
8.. index:: object: function
9
10There are a few functions specific to Python functions.
11
12
Georg Brandl60203b42010-10-06 10:11:56 +000013.. c:type:: PyFunctionObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000014
15 The C structure used for functions.
16
17
Georg Brandl60203b42010-10-06 10:11:56 +000018.. c:var:: PyTypeObject PyFunction_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000019
20 .. index:: single: MethodType (in module types)
21
Georg Brandl60203b42010-10-06 10:11:56 +000022 This is an instance of :c:type:`PyTypeObject` and represents the Python function
Georg Brandl54a3faa2008-01-20 09:30:57 +000023 type. It is exposed to Python programmers as ``types.FunctionType``.
24
25
Georg Brandl60203b42010-10-06 10:11:56 +000026.. c:function:: int PyFunction_Check(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000027
Georg Brandl60203b42010-10-06 10:11:56 +000028 Return true if *o* is a function object (has type :c:data:`PyFunction_Type`).
Antonio Cuni315fc522021-01-06 12:38:26 +010029 The parameter must not be ``NULL``. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000030
31
Georg Brandl60203b42010-10-06 10:11:56 +000032.. c:function:: PyObject* PyFunction_New(PyObject *code, PyObject *globals)
Georg Brandl54a3faa2008-01-20 09:30:57 +000033
34 Return a new function object associated with the code object *code*. *globals*
35 must be a dictionary with the global variables accessible to the function.
36
Benjamin Peterson38723502016-05-09 23:43:53 -070037 The function's docstring and name are retrieved from the code object. *__module__*
38 is retrieved from *globals*. The argument defaults, annotations and closure are
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020039 set to ``NULL``. *__qualname__* is set to the same value as the function's name.
Georg Brandl54a3faa2008-01-20 09:30:57 +000040
41
Antoine Pitrou86a36b52011-11-25 18:56:07 +010042.. c:function:: PyObject* PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
43
Martin Panterc04fb562016-02-10 05:44:01 +000044 As :c:func:`PyFunction_New`, but also allows setting the function object's
Serhiy Storchakae835b312019-10-30 21:37:16 +020045 ``__qualname__`` attribute. *qualname* should be a unicode object or ``NULL``;
46 if ``NULL``, the ``__qualname__`` attribute is set to the same value as its
Antoine Pitrou86a36b52011-11-25 18:56:07 +010047 ``__name__`` attribute.
48
49 .. versionadded:: 3.3
50
51
Georg Brandl60203b42010-10-06 10:11:56 +000052.. c:function:: PyObject* PyFunction_GetCode(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000053
54 Return the code object associated with the function object *op*.
55
56
Georg Brandl60203b42010-10-06 10:11:56 +000057.. c:function:: PyObject* PyFunction_GetGlobals(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000058
59 Return the globals dictionary associated with the function object *op*.
60
61
Georg Brandl60203b42010-10-06 10:11:56 +000062.. c:function:: PyObject* PyFunction_GetModule(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000063
Victor Stinner44085a32021-02-18 19:20:16 +010064 Return a :term:`borrowed reference` to the *__module__* attribute of the
65 function object *op*. It can be *NULL*.
66
67 This is normally a string containing the module name, but can be set to any
68 other object by Python code.
Georg Brandl54a3faa2008-01-20 09:30:57 +000069
70
Georg Brandl60203b42010-10-06 10:11:56 +000071.. c:function:: PyObject* PyFunction_GetDefaults(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000072
73 Return the argument default values of the function object *op*. This can be a
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020074 tuple of arguments or ``NULL``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000075
76
Georg Brandl60203b42010-10-06 10:11:56 +000077.. c:function:: int PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
79 Set the argument default values for the function object *op*. *defaults* must be
Serhiy Storchakae835b312019-10-30 21:37:16 +020080 ``Py_None`` or a tuple.
Georg Brandl54a3faa2008-01-20 09:30:57 +000081
82 Raises :exc:`SystemError` and returns ``-1`` on failure.
83
84
Georg Brandl60203b42010-10-06 10:11:56 +000085.. c:function:: PyObject* PyFunction_GetClosure(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000086
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020087 Return the closure associated with the function object *op*. This can be ``NULL``
Georg Brandl54a3faa2008-01-20 09:30:57 +000088 or a tuple of cell objects.
89
90
Georg Brandl60203b42010-10-06 10:11:56 +000091.. c:function:: int PyFunction_SetClosure(PyObject *op, PyObject *closure)
Georg Brandl54a3faa2008-01-20 09:30:57 +000092
93 Set the closure associated with the function object *op*. *closure* must be
Serhiy Storchakae835b312019-10-30 21:37:16 +020094 ``Py_None`` or a tuple of cell objects.
Georg Brandl54a3faa2008-01-20 09:30:57 +000095
96 Raises :exc:`SystemError` and returns ``-1`` on failure.
Alexandre Vassalottib0c81652008-07-13 22:26:50 +000097
98
Georg Brandl60203b42010-10-06 10:11:56 +000099.. c:function:: PyObject *PyFunction_GetAnnotations(PyObject *op)
Alexandre Vassalottib0c81652008-07-13 22:26:50 +0000100
101 Return the annotations of the function object *op*. This can be a
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200102 mutable dictionary or ``NULL``.
Alexandre Vassalottib0c81652008-07-13 22:26:50 +0000103
104
Georg Brandl60203b42010-10-06 10:11:56 +0000105.. c:function:: int PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
Alexandre Vassalottib0c81652008-07-13 22:26:50 +0000106
107 Set the annotations for the function object *op*. *annotations*
Serhiy Storchakae835b312019-10-30 21:37:16 +0200108 must be a dictionary or ``Py_None``.
Alexandre Vassalotti3065b872008-07-13 22:28:42 +0000109
110 Raises :exc:`SystemError` and returns ``-1`` on failure.