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 |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 17 | the abstract object protocol (including :c:func:`PyObject_CallMethod`, |
| 18 | :c:func:`PyObject_RichCompareBool`, :c:func:`PyObject_Hash`, |
| 19 | :c:func:`PyObject_Repr`, :c:func:`PyObject_IsTrue`, :c:func:`PyObject_Print`, and |
| 20 | :c:func:`PyObject_GetIter`) or the abstract number protocol (including |
| 21 | :c:func:`PyNumber_And`, :c:func:`PyNumber_Subtract`, :c:func:`PyNumber_Or`, |
| 22 | :c:func:`PyNumber_Xor`, :c:func:`PyNumber_InPlaceAnd`, |
| 23 | :c:func:`PyNumber_InPlaceSubtract`, :c:func:`PyNumber_InPlaceOr`, and |
| 24 | :c:func:`PyNumber_InPlaceXor`). |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 25 | |
| 26 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 27 | .. c:type:: PySetObject |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 28 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 29 | This subtype of :c:type:`PyObject` is used to hold the internal data for both |
| 30 | :class:`set` and :class:`frozenset` objects. It is like a :c:type:`PyDictObject` |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 38 | .. c:var:: PyTypeObject PySet_Type |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 39 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 40 | This is an instance of :c:type:`PyTypeObject` representing the Python |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 41 | :class:`set` type. |
| 42 | |
| 43 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 44 | .. c:var:: PyTypeObject PyFrozenSet_Type |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 45 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 46 | This is an instance of :c:type:`PyTypeObject` representing the Python |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 53 | .. c:function:: int PySet_Check(PyObject *p) |
Christian Heimes | fd66e51 | 2008-01-29 12:18:50 +0000 | [diff] [blame] | 54 | |
| 55 | Return true if *p* is a :class:`set` object or an instance of a subtype. |
| 56 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 57 | .. c:function:: int PyFrozenSet_Check(PyObject *p) |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 58 | |
| 59 | Return true if *p* is a :class:`frozenset` object or an instance of a |
| 60 | subtype. |
| 61 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 62 | .. c:function:: int PyAnySet_Check(PyObject *p) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 63 | |
| 64 | Return true if *p* is a :class:`set` object, a :class:`frozenset` object, or an |
| 65 | instance of a subtype. |
| 66 | |
| 67 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 68 | .. c:function:: int PyAnySet_CheckExact(PyObject *p) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 69 | |
| 70 | Return true if *p* is a :class:`set` object or a :class:`frozenset` object but |
| 71 | not an instance of a subtype. |
| 72 | |
| 73 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 74 | .. c:function:: int PyFrozenSet_CheckExact(PyObject *p) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 75 | |
| 76 | Return true if *p* is a :class:`frozenset` object but not an instance of a |
| 77 | subtype. |
| 78 | |
| 79 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 80 | .. c:function:: PyObject* PySet_New(PyObject *iterable) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 81 | |
| 82 | Return a new :class:`set` containing objects returned by the *iterable*. The |
| 83 | *iterable* may be *NULL* to create a new empty set. Return the new set on |
| 84 | success or *NULL* on failure. Raise :exc:`TypeError` if *iterable* is not |
| 85 | actually iterable. The constructor is also useful for copying a set |
| 86 | (``c=set(s)``). |
| 87 | |
| 88 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 89 | .. c:function:: PyObject* PyFrozenSet_New(PyObject *iterable) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 90 | |
| 91 | Return a new :class:`frozenset` containing objects returned by the *iterable*. |
| 92 | The *iterable* may be *NULL* to create a new empty frozenset. Return the new |
| 93 | set on success or *NULL* on failure. Raise :exc:`TypeError` if *iterable* is |
| 94 | not actually iterable. |
| 95 | |
Christian Heimes | fd66e51 | 2008-01-29 12:18:50 +0000 | [diff] [blame] | 96 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 97 | The following functions and macros are available for instances of :class:`set` |
| 98 | or :class:`frozenset` or instances of their subtypes. |
| 99 | |
| 100 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 101 | .. c:function:: Py_ssize_t PySet_Size(PyObject *anyset) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 102 | |
| 103 | .. index:: builtin: len |
| 104 | |
| 105 | Return the length of a :class:`set` or :class:`frozenset` object. Equivalent to |
| 106 | ``len(anyset)``. Raises a :exc:`PyExc_SystemError` if *anyset* is not a |
| 107 | :class:`set`, :class:`frozenset`, or an instance of a subtype. |
| 108 | |
| 109 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 110 | .. c:function:: Py_ssize_t PySet_GET_SIZE(PyObject *anyset) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 111 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 112 | Macro form of :c:func:`PySet_Size` without error checking. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 113 | |
| 114 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 115 | .. c:function:: int PySet_Contains(PyObject *anyset, PyObject *key) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 116 | |
| 117 | Return 1 if found, 0 if not found, and -1 if an error is encountered. Unlike |
| 118 | the Python :meth:`__contains__` method, this function does not automatically |
| 119 | convert unhashable sets into temporary frozensets. Raise a :exc:`TypeError` if |
| 120 | the *key* is unhashable. Raise :exc:`PyExc_SystemError` if *anyset* is not a |
| 121 | :class:`set`, :class:`frozenset`, or an instance of a subtype. |
| 122 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 123 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 124 | .. c:function:: int PySet_Add(PyObject *set, PyObject *key) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 125 | |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 126 | Add *key* to a :class:`set` instance. Also works with :class:`frozenset` |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 127 | instances (like :c:func:`PyTuple_SetItem` it can be used to fill-in the values |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 128 | of brand new frozensets before they are exposed to other code). Return 0 on |
| 129 | success or -1 on failure. Raise a :exc:`TypeError` if the *key* is |
| 130 | unhashable. Raise a :exc:`MemoryError` if there is no room to grow. Raise a |
| 131 | :exc:`SystemError` if *set* is an not an instance of :class:`set` or its |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 132 | subtype. |
| 133 | |
Christian Heimes | fd66e51 | 2008-01-29 12:18:50 +0000 | [diff] [blame] | 134 | |
| 135 | The following functions are available for instances of :class:`set` or its |
| 136 | subtypes but not for instances of :class:`frozenset` or its subtypes. |
| 137 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 138 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 139 | .. c:function:: int PySet_Discard(PyObject *set, PyObject *key) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 140 | |
| 141 | Return 1 if found and removed, 0 if not found (no action taken), and -1 if an |
| 142 | error is encountered. Does not raise :exc:`KeyError` for missing keys. Raise a |
| 143 | :exc:`TypeError` if the *key* is unhashable. Unlike the Python :meth:`discard` |
| 144 | method, this function does not automatically convert unhashable sets into |
| 145 | temporary frozensets. Raise :exc:`PyExc_SystemError` if *set* is an not an |
| 146 | instance of :class:`set` or its subtype. |
| 147 | |
| 148 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 149 | .. c:function:: PyObject* PySet_Pop(PyObject *set) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 150 | |
| 151 | Return a new reference to an arbitrary object in the *set*, and removes the |
| 152 | object from the *set*. Return *NULL* on failure. Raise :exc:`KeyError` if the |
| 153 | set is empty. Raise a :exc:`SystemError` if *set* is an not an instance of |
| 154 | :class:`set` or its subtype. |
| 155 | |
| 156 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 157 | .. c:function:: int PySet_Clear(PyObject *set) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 158 | |
| 159 | Empty an existing set of all elements. |
Antoine Pitrou | 093ce9c | 2011-12-16 11:24:27 +0100 | [diff] [blame] | 160 | |
| 161 | |
| 162 | .. c:function:: int PySet_ClearFreeList() |
| 163 | |
| 164 | Clear the free list. Return the total number of freed items. |
| 165 | |
| 166 | .. versionadded:: 3.3 |