Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 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 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 13 | .. c:type:: PyFunctionObject |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 14 | |
| 15 | The C structure used for functions. |
| 16 | |
| 17 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 18 | .. c:var:: PyTypeObject PyFunction_Type |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 19 | |
| 20 | .. index:: single: MethodType (in module types) |
| 21 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 22 | This is an instance of :c:type:`PyTypeObject` and represents the Python function |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 23 | type. It is exposed to Python programmers as ``types.FunctionType``. |
| 24 | |
| 25 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 26 | .. c:function:: int PyFunction_Check(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 27 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 28 | Return true if *o* is a function object (has type :c:data:`PyFunction_Type`). |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 29 | The parameter must not be *NULL*. |
| 30 | |
| 31 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 32 | .. c:function:: PyObject* PyFunction_New(PyObject *code, PyObject *globals) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +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 | |
| 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 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 41 | .. c:function:: PyObject* PyFunction_GetCode(PyObject *op) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 42 | |
| 43 | Return the code object associated with the function object *op*. |
| 44 | |
| 45 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 46 | .. c:function:: PyObject* PyFunction_GetGlobals(PyObject *op) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 47 | |
| 48 | Return the globals dictionary associated with the function object *op*. |
| 49 | |
| 50 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 51 | .. c:function:: PyObject* PyFunction_GetModule(PyObject *op) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 52 | |
| 53 | Return the *__module__* attribute of the function object *op*. This is normally |
| 54 | a string containing the module name, but can be set to any other object by |
| 55 | Python code. |
| 56 | |
| 57 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 58 | .. c:function:: PyObject* PyFunction_GetDefaults(PyObject *op) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 59 | |
| 60 | Return the argument default values of the function object *op*. This can be a |
| 61 | tuple of arguments or *NULL*. |
| 62 | |
| 63 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 64 | .. c:function:: int PyFunction_SetDefaults(PyObject *op, PyObject *defaults) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 65 | |
| 66 | Set the argument default values for the function object *op*. *defaults* must be |
| 67 | *Py_None* or a tuple. |
| 68 | |
| 69 | Raises :exc:`SystemError` and returns ``-1`` on failure. |
| 70 | |
| 71 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 72 | .. c:function:: PyObject* PyFunction_GetClosure(PyObject *op) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 73 | |
| 74 | Return the closure associated with the function object *op*. This can be *NULL* |
| 75 | or a tuple of cell objects. |
| 76 | |
| 77 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 78 | .. c:function:: int PyFunction_SetClosure(PyObject *op, PyObject *closure) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 79 | |
| 80 | Set the closure associated with the function object *op*. *closure* must be |
| 81 | *Py_None* or a tuple of cell objects. |
| 82 | |
| 83 | Raises :exc:`SystemError` and returns ``-1`` on failure. |