blob: ad9832233f74d9f9f40dc5b80f7a12d785d7e98b [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
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`).
Georg Brandl54a3faa2008-01-20 09:30:57 +000029 The parameter must not be *NULL*.
30
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
37 The function's docstring, name and *__module__* are retrieved from the code
38 object, the argument defaults and closure are set to *NULL*.
39
40
Antoine Pitrou86a36b52011-11-25 18:56:07 +010041.. c:function:: PyObject* PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
42
43 As :c:func:`PyFunction_New`, but also allows to set the function object's
44 ``__qualname__`` attribute. *qualname* should be a unicode object or NULL;
45 if NULL, the ``__qualname__`` attribute is set to the same value as its
46 ``__name__`` attribute.
47
48 .. versionadded:: 3.3
49
50
Georg Brandl60203b42010-10-06 10:11:56 +000051.. c:function:: PyObject* PyFunction_GetCode(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000052
53 Return the code object associated with the function object *op*.
54
55
Georg Brandl60203b42010-10-06 10:11:56 +000056.. c:function:: PyObject* PyFunction_GetGlobals(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000057
58 Return the globals dictionary associated with the function object *op*.
59
60
Georg Brandl60203b42010-10-06 10:11:56 +000061.. c:function:: PyObject* PyFunction_GetModule(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000062
63 Return the *__module__* attribute of the function object *op*. This is normally
64 a string containing the module name, but can be set to any other object by
65 Python code.
66
67
Georg Brandl60203b42010-10-06 10:11:56 +000068.. c:function:: PyObject* PyFunction_GetDefaults(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000069
70 Return the argument default values of the function object *op*. This can be a
71 tuple of arguments or *NULL*.
72
73
Georg Brandl60203b42010-10-06 10:11:56 +000074.. c:function:: int PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Georg Brandl54a3faa2008-01-20 09:30:57 +000075
76 Set the argument default values for the function object *op*. *defaults* must be
77 *Py_None* or a tuple.
78
79 Raises :exc:`SystemError` and returns ``-1`` on failure.
80
81
Georg Brandl60203b42010-10-06 10:11:56 +000082.. c:function:: PyObject* PyFunction_GetClosure(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000083
84 Return the closure associated with the function object *op*. This can be *NULL*
85 or a tuple of cell objects.
86
87
Georg Brandl60203b42010-10-06 10:11:56 +000088.. c:function:: int PyFunction_SetClosure(PyObject *op, PyObject *closure)
Georg Brandl54a3faa2008-01-20 09:30:57 +000089
90 Set the closure associated with the function object *op*. *closure* must be
91 *Py_None* or a tuple of cell objects.
92
93 Raises :exc:`SystemError` and returns ``-1`` on failure.
Alexandre Vassalottib0c81652008-07-13 22:26:50 +000094
95
Georg Brandl60203b42010-10-06 10:11:56 +000096.. c:function:: PyObject *PyFunction_GetAnnotations(PyObject *op)
Alexandre Vassalottib0c81652008-07-13 22:26:50 +000097
98 Return the annotations of the function object *op*. This can be a
99 mutable dictionary or *NULL*.
100
101
Georg Brandl60203b42010-10-06 10:11:56 +0000102.. c:function:: int PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
Alexandre Vassalottib0c81652008-07-13 22:26:50 +0000103
104 Set the annotations for the function object *op*. *annotations*
105 must be a dictionary or *Py_None*.
Alexandre Vassalotti3065b872008-07-13 22:28:42 +0000106
107 Raises :exc:`SystemError` and returns ``-1`` on failure.