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.

108 lines
3.3 KiB

  1. .. highlightlang:: 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*.
  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 the *__module__* attribute of the function object *op*. This is normally
  34. a string containing the module name, but can be set to any other object by
  35. Python code.
  36. .. c:function:: PyObject* PyFunction_GetDefaults(PyObject *op)
  37. Return the argument default values of the function object *op*. This can be a
  38. tuple of arguments or *NULL*.
  39. .. c:function:: int PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
  40. Set the argument default values for the function object *op*. *defaults* must be
  41. *Py_None* or a tuple.
  42. Raises :exc:`SystemError` and returns ``-1`` on failure.
  43. .. c:function:: PyObject* PyFunction_GetClosure(PyObject *op)
  44. Return the closure associated with the function object *op*. This can be *NULL*
  45. or a tuple of cell objects.
  46. .. c:function:: int PyFunction_SetClosure(PyObject *op, PyObject *closure)
  47. Set the closure associated with the function object *op*. *closure* must be
  48. *Py_None* or a tuple of cell objects.
  49. Raises :exc:`SystemError` and returns ``-1`` on failure.
  50. .. c:function:: PyObject *PyFunction_GetAnnotations(PyObject *op)
  51. Return the annotations of the function object *op*. This can be a
  52. mutable dictionary or *NULL*.
  53. .. c:function:: int PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
  54. Set the annotations for the function object *op*. *annotations*
  55. must be a dictionary or *Py_None*.
  56. Raises :exc:`SystemError` and returns ``-1`` on failure.