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