/Doc/c-api/set.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 175 lines · 110 code · 65 blank · 0 comment · 0 complexity · 87db4d97fcff81afb5303a823baff97e MD5 · raw file

  1. .. highlightlang:: c
  2. .. _setobjects:
  3. Set Objects
  4. -----------
  5. .. sectionauthor:: Raymond D. Hettinger <python@rcn.com>
  6. .. index::
  7. object: set
  8. object: frozenset
  9. .. versionadded:: 2.5
  10. This section details the public API for :class:`set` and :class:`frozenset`
  11. objects. Any functionality not listed below is best accessed using the either
  12. the abstract object protocol (including :cfunc:`PyObject_CallMethod`,
  13. :cfunc:`PyObject_RichCompareBool`, :cfunc:`PyObject_Hash`,
  14. :cfunc:`PyObject_Repr`, :cfunc:`PyObject_IsTrue`, :cfunc:`PyObject_Print`, and
  15. :cfunc:`PyObject_GetIter`) or the abstract number protocol (including
  16. :cfunc:`PyNumber_And`, :cfunc:`PyNumber_Subtract`, :cfunc:`PyNumber_Or`,
  17. :cfunc:`PyNumber_Xor`, :cfunc:`PyNumber_InPlaceAnd`,
  18. :cfunc:`PyNumber_InPlaceSubtract`, :cfunc:`PyNumber_InPlaceOr`, and
  19. :cfunc:`PyNumber_InPlaceXor`).
  20. .. ctype:: PySetObject
  21. This subtype of :ctype:`PyObject` is used to hold the internal data for both
  22. :class:`set` and :class:`frozenset` objects. It is like a :ctype:`PyDictObject`
  23. in that it is a fixed size for small sets (much like tuple storage) and will
  24. point to a separate, variable sized block of memory for medium and large sized
  25. sets (much like list storage). None of the fields of this structure should be
  26. considered public and are subject to change. All access should be done through
  27. the documented API rather than by manipulating the values in the structure.
  28. .. cvar:: PyTypeObject PySet_Type
  29. This is an instance of :ctype:`PyTypeObject` representing the Python
  30. :class:`set` type.
  31. .. cvar:: PyTypeObject PyFrozenSet_Type
  32. This is an instance of :ctype:`PyTypeObject` representing the Python
  33. :class:`frozenset` type.
  34. The following type check macros work on pointers to any Python object. Likewise,
  35. the constructor functions work with any iterable Python object.
  36. .. cfunction:: int PySet_Check(PyObject *p)
  37. Return true if *p* is a :class:`set` object or an instance of a subtype.
  38. .. versionadded:: 2.6
  39. .. cfunction:: int PyFrozenSet_Check(PyObject *p)
  40. Return true if *p* is a :class:`frozenset` object or an instance of a
  41. subtype.
  42. .. versionadded:: 2.6
  43. .. cfunction:: int PyAnySet_Check(PyObject *p)
  44. Return true if *p* is a :class:`set` object, a :class:`frozenset` object, or an
  45. instance of a subtype.
  46. .. cfunction:: int PyAnySet_CheckExact(PyObject *p)
  47. Return true if *p* is a :class:`set` object or a :class:`frozenset` object but
  48. not an instance of a subtype.
  49. .. cfunction:: int PyFrozenSet_CheckExact(PyObject *p)
  50. Return true if *p* is a :class:`frozenset` object but not an instance of a
  51. subtype.
  52. .. cfunction:: PyObject* PySet_New(PyObject *iterable)
  53. Return a new :class:`set` containing objects returned by the *iterable*. The
  54. *iterable* may be *NULL* to create a new empty set. Return the new set on
  55. success or *NULL* on failure. Raise :exc:`TypeError` if *iterable* is not
  56. actually iterable. The constructor is also useful for copying a set
  57. (``c=set(s)``).
  58. .. cfunction:: PyObject* PyFrozenSet_New(PyObject *iterable)
  59. Return a new :class:`frozenset` containing objects returned by the *iterable*.
  60. The *iterable* may be *NULL* to create a new empty frozenset. Return the new
  61. set on success or *NULL* on failure. Raise :exc:`TypeError` if *iterable* is
  62. not actually iterable.
  63. .. versionchanged:: 2.6
  64. Now guaranteed to return a brand-new :class:`frozenset`. Formerly,
  65. frozensets of zero-length were a singleton. This got in the way of
  66. building-up new frozensets with :meth:`PySet_Add`.
  67. The following functions and macros are available for instances of :class:`set`
  68. or :class:`frozenset` or instances of their subtypes.
  69. .. cfunction:: Py_ssize_t PySet_Size(PyObject *anyset)
  70. .. index:: builtin: len
  71. Return the length of a :class:`set` or :class:`frozenset` object. Equivalent to
  72. ``len(anyset)``. Raises a :exc:`PyExc_SystemError` if *anyset* is not a
  73. :class:`set`, :class:`frozenset`, or an instance of a subtype.
  74. .. versionchanged:: 2.5
  75. This function returned an :ctype:`int`. This might require changes in
  76. your code for properly supporting 64-bit systems.
  77. .. cfunction:: Py_ssize_t PySet_GET_SIZE(PyObject *anyset)
  78. Macro form of :cfunc:`PySet_Size` without error checking.
  79. .. cfunction:: int PySet_Contains(PyObject *anyset, PyObject *key)
  80. Return 1 if found, 0 if not found, and -1 if an error is encountered. Unlike
  81. the Python :meth:`__contains__` method, this function does not automatically
  82. convert unhashable sets into temporary frozensets. Raise a :exc:`TypeError` if
  83. the *key* is unhashable. Raise :exc:`PyExc_SystemError` if *anyset* is not a
  84. :class:`set`, :class:`frozenset`, or an instance of a subtype.
  85. .. cfunction:: int PySet_Add(PyObject *set, PyObject *key)
  86. Add *key* to a :class:`set` instance. Does not apply to :class:`frozenset`
  87. instances. Return 0 on success or -1 on failure. Raise a :exc:`TypeError` if
  88. the *key* is unhashable. Raise a :exc:`MemoryError` if there is no room to grow.
  89. Raise a :exc:`SystemError` if *set* is an not an instance of :class:`set` or its
  90. subtype.
  91. .. versionchanged:: 2.6
  92. Now works with instances of :class:`frozenset` or its subtypes.
  93. Like :cfunc:`PyTuple_SetItem` in that it can be used to fill-in the
  94. values of brand new frozensets before they are exposed to other code.
  95. The following functions are available for instances of :class:`set` or its
  96. subtypes but not for instances of :class:`frozenset` or its subtypes.
  97. .. cfunction:: int PySet_Discard(PyObject *set, PyObject *key)
  98. Return 1 if found and removed, 0 if not found (no action taken), and -1 if an
  99. error is encountered. Does not raise :exc:`KeyError` for missing keys. Raise a
  100. :exc:`TypeError` if the *key* is unhashable. Unlike the Python :meth:`discard`
  101. method, this function does not automatically convert unhashable sets into
  102. temporary frozensets. Raise :exc:`PyExc_SystemError` if *set* is an not an
  103. instance of :class:`set` or its subtype.
  104. .. cfunction:: PyObject* PySet_Pop(PyObject *set)
  105. Return a new reference to an arbitrary object in the *set*, and removes the
  106. object from the *set*. Return *NULL* on failure. Raise :exc:`KeyError` if the
  107. set is empty. Raise a :exc:`SystemError` if *set* is an not an instance of
  108. :class:`set` or its subtype.
  109. .. cfunction:: int PySet_Clear(PyObject *set)
  110. Empty an existing set of all elements.