Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: c |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 2 | |
| 3 | .. _function-objects: |
| 4 | |
| 5 | Function Objects |
| 6 | ---------------- |
| 7 | |
| 8 | .. index:: object: function |
| 9 | |
| 10 | There are a few functions specific to Python functions. |
| 11 | |
| 12 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 13 | .. c:type:: PyFunctionObject |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 14 | |
| 15 | The C structure used for functions. |
| 16 | |
| 17 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 18 | .. c:var:: PyTypeObject PyFunction_Type |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 19 | |
| 20 | .. index:: single: MethodType (in module types) |
| 21 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 22 | This is an instance of :c:type:`PyTypeObject` and represents the Python function |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 23 | type. It is exposed to Python programmers as ``types.FunctionType``. |
| 24 | |
| 25 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 26 | .. c:function:: int PyFunction_Check(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 27 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 28 | Return true if *o* is a function object (has type :c:data:`PyFunction_Type`). |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 29 | The parameter must not be ``NULL``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 30 | |
| 31 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 32 | .. c:function:: PyObject* PyFunction_New(PyObject *code, PyObject *globals) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 33 | |
| 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 Peterson | 3872350 | 2016-05-09 23:43:53 -0700 | [diff] [blame] | 37 | 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 Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 39 | set to ``NULL``. *__qualname__* is set to the same value as the function's name. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 40 | |
| 41 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 42 | .. c:function:: PyObject* PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname) |
| 43 | |
Martin Panter | c04fb56 | 2016-02-10 05:44:01 +0000 | [diff] [blame] | 44 | As :c:func:`PyFunction_New`, but also allows setting the function object's |
Serhiy Storchaka | e835b31 | 2019-10-30 21:37:16 +0200 | [diff] [blame] | 45 | ``__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 Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 47 | ``__name__`` attribute. |
| 48 | |
| 49 | .. versionadded:: 3.3 |
| 50 | |
| 51 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 52 | .. c:function:: PyObject* PyFunction_GetCode(PyObject *op) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 53 | |
| 54 | Return the code object associated with the function object *op*. |
| 55 | |
| 56 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 57 | .. c:function:: PyObject* PyFunction_GetGlobals(PyObject *op) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 58 | |
| 59 | Return the globals dictionary associated with the function object *op*. |
| 60 | |
| 61 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 62 | .. c:function:: PyObject* PyFunction_GetModule(PyObject *op) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 63 | |
| 64 | Return the *__module__* attribute of the function object *op*. This is normally |
| 65 | a string containing the module name, but can be set to any other object by |
| 66 | Python code. |
| 67 | |
| 68 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 69 | .. c:function:: PyObject* PyFunction_GetDefaults(PyObject *op) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 70 | |
| 71 | Return the argument default values of the function object *op*. This can be a |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 72 | tuple of arguments or ``NULL``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 73 | |
| 74 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 75 | .. c:function:: int PyFunction_SetDefaults(PyObject *op, PyObject *defaults) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 76 | |
| 77 | Set the argument default values for the function object *op*. *defaults* must be |
Serhiy Storchaka | e835b31 | 2019-10-30 21:37:16 +0200 | [diff] [blame] | 78 | ``Py_None`` or a tuple. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 79 | |
| 80 | Raises :exc:`SystemError` and returns ``-1`` on failure. |
| 81 | |
| 82 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 83 | .. c:function:: PyObject* PyFunction_GetClosure(PyObject *op) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 84 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 85 | Return the closure associated with the function object *op*. This can be ``NULL`` |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 86 | or a tuple of cell objects. |
| 87 | |
| 88 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 89 | .. c:function:: int PyFunction_SetClosure(PyObject *op, PyObject *closure) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 90 | |
| 91 | Set the closure associated with the function object *op*. *closure* must be |
Serhiy Storchaka | e835b31 | 2019-10-30 21:37:16 +0200 | [diff] [blame] | 92 | ``Py_None`` or a tuple of cell objects. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 93 | |
| 94 | Raises :exc:`SystemError` and returns ``-1`` on failure. |
Alexandre Vassalotti | b0c8165 | 2008-07-13 22:26:50 +0000 | [diff] [blame] | 95 | |
| 96 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 97 | .. c:function:: PyObject *PyFunction_GetAnnotations(PyObject *op) |
Alexandre Vassalotti | b0c8165 | 2008-07-13 22:26:50 +0000 | [diff] [blame] | 98 | |
| 99 | Return the annotations of the function object *op*. This can be a |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 100 | mutable dictionary or ``NULL``. |
Alexandre Vassalotti | b0c8165 | 2008-07-13 22:26:50 +0000 | [diff] [blame] | 101 | |
| 102 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 103 | .. c:function:: int PyFunction_SetAnnotations(PyObject *op, PyObject *annotations) |
Alexandre Vassalotti | b0c8165 | 2008-07-13 22:26:50 +0000 | [diff] [blame] | 104 | |
| 105 | Set the annotations for the function object *op*. *annotations* |
Serhiy Storchaka | e835b31 | 2019-10-30 21:37:16 +0200 | [diff] [blame] | 106 | must be a dictionary or ``Py_None``. |
Alexandre Vassalotti | 3065b87 | 2008-07-13 22:28:42 +0000 | [diff] [blame] | 107 | |
| 108 | Raises :exc:`SystemError` and returns ``-1`` on failure. |