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