Issue #5262: Improved fix.
[python.git] / Doc / c-api / function.rst
blobe9ed2abdaa74f3aa8f7e64d4b6cbafadf1aa010c
1 .. highlightlang:: c
3 .. _function-objects:
5 Function Objects
6 ----------------
8 .. index:: object: function
10 There are a few functions specific to Python functions.
13 .. ctype:: PyFunctionObject
15    The C structure used for functions.
18 .. cvar:: PyTypeObject PyFunction_Type
20    .. index:: single: MethodType (in module types)
22    This is an instance of :ctype:`PyTypeObject` and represents the Python function
23    type.  It is exposed to Python programmers as ``types.FunctionType``.
26 .. cfunction:: int PyFunction_Check(PyObject *o)
28    Return true if *o* is a function object (has type :cdata:`PyFunction_Type`).
29    The parameter must not be *NULL*.
32 .. cfunction:: PyObject* PyFunction_New(PyObject *code, PyObject *globals)
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.
37    The function's docstring, name and *__module__* are retrieved from the code
38    object, the argument defaults and closure are set to *NULL*.
41 .. cfunction:: PyObject* PyFunction_GetCode(PyObject *op)
43    Return the code object associated with the function object *op*.
46 .. cfunction:: PyObject* PyFunction_GetGlobals(PyObject *op)
48    Return the globals dictionary associated with the function object *op*.
51 .. cfunction:: PyObject* PyFunction_GetModule(PyObject *op)
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.
58 .. cfunction:: PyObject* PyFunction_GetDefaults(PyObject *op)
60    Return the argument default values of the function object *op*. This can be a
61    tuple of arguments or *NULL*.
64 .. cfunction:: int PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
66    Set the argument default values for the function object *op*. *defaults* must be
67    *Py_None* or a tuple.
69    Raises :exc:`SystemError` and returns ``-1`` on failure.
72 .. cfunction:: PyObject* PyFunction_GetClosure(PyObject *op)
74    Return the closure associated with the function object *op*. This can be *NULL*
75    or a tuple of cell objects.
78 .. cfunction:: int PyFunction_SetClosure(PyObject *op, PyObject *closure)
80    Set the closure associated with the function object *op*. *closure* must be
81    *Py_None* or a tuple of cell objects.
83    Raises :exc:`SystemError` and returns ``-1`` on failure.