1.. highlight:: c 2 3.. _function-objects: 4 5Function Objects 6---------------- 7 8.. index:: pair: object; function 9 10There are a few functions specific to Python functions. 11 12 13.. c:type:: PyFunctionObject 14 15 The C structure used for functions. 16 17 18.. c:var:: PyTypeObject PyFunction_Type 19 20 .. index:: single: MethodType (in module types) 21 22 This is an instance of :c:type:`PyTypeObject` and represents the Python function 23 type. It is exposed to Python programmers as ``types.FunctionType``. 24 25 26.. c:function:: int PyFunction_Check(PyObject *o) 27 28 Return true if *o* is a function object (has type :c:data:`PyFunction_Type`). 29 The parameter must not be ``NULL``. This function always succeeds. 30 31 32.. c:function:: PyObject* PyFunction_New(PyObject *code, PyObject *globals) 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 and name are retrieved from the code object. *__module__* 38 is retrieved from *globals*. The argument defaults, annotations and closure are 39 set to ``NULL``. *__qualname__* is set to the same value as the code object's 40 ``co_qualname`` field. 41 42 43.. c:function:: PyObject* PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname) 44 45 As :c:func:`PyFunction_New`, but also allows setting the function object's 46 ``__qualname__`` attribute. *qualname* should be a unicode object or ``NULL``; 47 if ``NULL``, the ``__qualname__`` attribute is set to the same value as the 48 code object's ``co_qualname`` field. 49 50 .. versionadded:: 3.3 51 52 53.. c:function:: PyObject* PyFunction_GetCode(PyObject *op) 54 55 Return the code object associated with the function object *op*. 56 57 58.. c:function:: PyObject* PyFunction_GetGlobals(PyObject *op) 59 60 Return the globals dictionary associated with the function object *op*. 61 62 63.. c:function:: PyObject* PyFunction_GetModule(PyObject *op) 64 65 Return a :term:`borrowed reference` to the *__module__* attribute of the 66 function object *op*. It can be *NULL*. 67 68 This is normally a string containing the module name, but can be set to any 69 other object by Python code. 70 71 72.. c:function:: PyObject* PyFunction_GetDefaults(PyObject *op) 73 74 Return the argument default values of the function object *op*. This can be a 75 tuple of arguments or ``NULL``. 76 77 78.. c:function:: int PyFunction_SetDefaults(PyObject *op, PyObject *defaults) 79 80 Set the argument default values for the function object *op*. *defaults* must be 81 ``Py_None`` or a tuple. 82 83 Raises :exc:`SystemError` and returns ``-1`` on failure. 84 85 86.. c:function:: PyObject* PyFunction_GetClosure(PyObject *op) 87 88 Return the closure associated with the function object *op*. This can be ``NULL`` 89 or a tuple of cell objects. 90 91 92.. c:function:: int PyFunction_SetClosure(PyObject *op, PyObject *closure) 93 94 Set the closure associated with the function object *op*. *closure* must be 95 ``Py_None`` or a tuple of cell objects. 96 97 Raises :exc:`SystemError` and returns ``-1`` on failure. 98 99 100.. c:function:: PyObject *PyFunction_GetAnnotations(PyObject *op) 101 102 Return the annotations of the function object *op*. This can be a 103 mutable dictionary or ``NULL``. 104 105 106.. c:function:: int PyFunction_SetAnnotations(PyObject *op, PyObject *annotations) 107 108 Set the annotations for the function object *op*. *annotations* 109 must be a dictionary or ``Py_None``. 110 111 Raises :exc:`SystemError` and returns ``-1`` on failure. 112