You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.4 KiB

  1. .. highlight:: c
  2. .. _function-objects:
  3. Function Objects
  4. ----------------
  5. .. index:: object: function
  6. There are a few functions specific to Python functions.
  7. .. c:type:: PyFunctionObject
  8. The C structure used for functions.
  9. .. c:var:: PyTypeObject PyFunction_Type
  10. .. index:: single: MethodType (in module types)
  11. This is an instance of :c:type:`PyTypeObject` and represents the Python function
  12. type. It is exposed to Python programmers as ``types.FunctionType``.
  13. .. c:function:: int PyFunction_Check(PyObject *o)
  14. Return true if *o* is a function object (has type :c:data:`PyFunction_Type`).
  15. The parameter must not be ``NULL``. This function always succeeds.
  16. .. c:function:: PyObject* PyFunction_New(PyObject *code, PyObject *globals)
  17. Return a new function object associated with the code object *code*. *globals*
  18. must be a dictionary with the global variables accessible to the function.
  19. The function's docstring and name are retrieved from the code object. *__module__*
  20. is retrieved from *globals*. The argument defaults, annotations and closure are
  21. set to ``NULL``. *__qualname__* is set to the same value as the function's name.
  22. .. c:function:: PyObject* PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
  23. As :c:func:`PyFunction_New`, but also allows setting the function object's
  24. ``__qualname__`` attribute. *qualname* should be a unicode object or ``NULL``;
  25. if ``NULL``, the ``__qualname__`` attribute is set to the same value as its
  26. ``__name__`` attribute.
  27. .. versionadded:: 3.3
  28. .. c:function:: PyObject* PyFunction_GetCode(PyObject *op)
  29. Return the code object associated with the function object *op*.
  30. .. c:function:: PyObject* PyFunction_GetGlobals(PyObject *op)
  31. Return the globals dictionary associated with the function object *op*.
  32. .. c:function:: PyObject* PyFunction_GetModule(PyObject *op)
  33. Return a :term:`borrowed reference` to the *__module__* attribute of the
  34. function object *op*. It can be *NULL*.
  35. This is normally a string containing the module name, but can be set to any
  36. other object by Python code.
  37. .. c:function:: PyObject* PyFunction_GetDefaults(PyObject *op)
  38. Return the argument default values of the function object *op*. This can be a
  39. tuple of arguments or ``NULL``.
  40. .. c:function:: int PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
  41. Set the argument default values for the function object *op*. *defaults* must be
  42. ``Py_None`` or a tuple.
  43. Raises :exc:`SystemError` and returns ``-1`` on failure.
  44. .. c:function:: PyObject* PyFunction_GetClosure(PyObject *op)
  45. Return the closure associated with the function object *op*. This can be ``NULL``
  46. or a tuple of cell objects.
  47. .. c:function:: int PyFunction_SetClosure(PyObject *op, PyObject *closure)
  48. Set the closure associated with the function object *op*. *closure* must be
  49. ``Py_None`` or a tuple of cell objects.
  50. Raises :exc:`SystemError` and returns ``-1`` on failure.
  51. .. c:function:: PyObject *PyFunction_GetAnnotations(PyObject *op)
  52. Return the annotations of the function object *op*. This can be a
  53. mutable dictionary or ``NULL``.
  54. .. c:function:: int PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
  55. Set the annotations for the function object *op*. *annotations*
  56. must be a dictionary or ``Py_None``.
  57. Raises :exc:`SystemError` and returns ``-1`` on failure.