blob: eca19c4d81647437f9781ed39f32f78d35aa70c5 [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
3.. _setobjects:
4
5Set Objects
6-----------
7
8.. sectionauthor:: Raymond D. Hettinger <python@rcn.com>
9
10
11.. index::
12 object: set
13 object: frozenset
14
15This section details the public API for :class:`set` and :class:`frozenset`
16objects. Any functionality not listed below is best accessed using the either
Georg Brandl60203b42010-10-06 10:11:56 +000017the 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 Brandl54a3faa2008-01-20 09:30:57 +000025
26
Georg Brandl60203b42010-10-06 10:11:56 +000027.. c:type:: PySetObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000028
Georg Brandl60203b42010-10-06 10:11:56 +000029 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 Brandl54a3faa2008-01-20 09:30:57 +000031 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 Brandl60203b42010-10-06 10:11:56 +000038.. c:var:: PyTypeObject PySet_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000039
Georg Brandl60203b42010-10-06 10:11:56 +000040 This is an instance of :c:type:`PyTypeObject` representing the Python
Georg Brandl54a3faa2008-01-20 09:30:57 +000041 :class:`set` type.
42
43
Georg Brandl60203b42010-10-06 10:11:56 +000044.. c:var:: PyTypeObject PyFrozenSet_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000045
Georg Brandl60203b42010-10-06 10:11:56 +000046 This is an instance of :c:type:`PyTypeObject` representing the Python
Georg Brandl54a3faa2008-01-20 09:30:57 +000047 :class:`frozenset` type.
48
49The following type check macros work on pointers to any Python object. Likewise,
50the constructor functions work with any iterable Python object.
51
52
Georg Brandl60203b42010-10-06 10:11:56 +000053.. c:function:: int PySet_Check(PyObject *p)
Christian Heimesfd66e512008-01-29 12:18:50 +000054
55 Return true if *p* is a :class:`set` object or an instance of a subtype.
Antonio Cuni315fc522021-01-06 12:38:26 +010056 This function always succeeds.
Christian Heimesfd66e512008-01-29 12:18:50 +000057
Georg Brandl60203b42010-10-06 10:11:56 +000058.. c:function:: int PyFrozenSet_Check(PyObject *p)
Christian Heimes15ebc882008-02-04 18:48:49 +000059
60 Return true if *p* is a :class:`frozenset` object or an instance of a
Antonio Cuni315fc522021-01-06 12:38:26 +010061 subtype. This function always succeeds.
Christian Heimes15ebc882008-02-04 18:48:49 +000062
Georg Brandl60203b42010-10-06 10:11:56 +000063.. c:function:: int PyAnySet_Check(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000064
65 Return true if *p* is a :class:`set` object, a :class:`frozenset` object, or an
Antonio Cuni315fc522021-01-06 12:38:26 +010066 instance of a subtype. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000067
Pablo Galindod439fb32021-02-20 18:03:08 +000068.. c:function:: int PySet_CheckExact(PyObject *p)
69
70 Return true if *p* is a :class:`set` object but not an instance of a
71 subtype. This function always succeeds.
72
73 .. versionadded:: 3.10
Georg Brandl54a3faa2008-01-20 09:30:57 +000074
Georg Brandl60203b42010-10-06 10:11:56 +000075.. c:function:: int PyAnySet_CheckExact(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000076
77 Return true if *p* is a :class:`set` object or a :class:`frozenset` object but
Antonio Cuni315fc522021-01-06 12:38:26 +010078 not an instance of a subtype. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000079
80
Georg Brandl60203b42010-10-06 10:11:56 +000081.. c:function:: int PyFrozenSet_CheckExact(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000082
83 Return true if *p* is a :class:`frozenset` object but not an instance of a
Antonio Cuni315fc522021-01-06 12:38:26 +010084 subtype. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000085
86
Georg Brandl60203b42010-10-06 10:11:56 +000087.. c:function:: PyObject* PySet_New(PyObject *iterable)
Georg Brandl54a3faa2008-01-20 09:30:57 +000088
89 Return a new :class:`set` containing objects returned by the *iterable*. The
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020090 *iterable* may be ``NULL`` to create a new empty set. Return the new set on
91 success or ``NULL`` on failure. Raise :exc:`TypeError` if *iterable* is not
Georg Brandl54a3faa2008-01-20 09:30:57 +000092 actually iterable. The constructor is also useful for copying a set
93 (``c=set(s)``).
94
95
Georg Brandl60203b42010-10-06 10:11:56 +000096.. c:function:: PyObject* PyFrozenSet_New(PyObject *iterable)
Georg Brandl54a3faa2008-01-20 09:30:57 +000097
98 Return a new :class:`frozenset` containing objects returned by the *iterable*.
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020099 The *iterable* may be ``NULL`` to create a new empty frozenset. Return the new
100 set on success or ``NULL`` on failure. Raise :exc:`TypeError` if *iterable* is
Georg Brandl54a3faa2008-01-20 09:30:57 +0000101 not actually iterable.
102
Christian Heimesfd66e512008-01-29 12:18:50 +0000103
Georg Brandl54a3faa2008-01-20 09:30:57 +0000104The following functions and macros are available for instances of :class:`set`
105or :class:`frozenset` or instances of their subtypes.
106
107
Georg Brandl60203b42010-10-06 10:11:56 +0000108.. c:function:: Py_ssize_t PySet_Size(PyObject *anyset)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000109
110 .. index:: builtin: len
111
112 Return the length of a :class:`set` or :class:`frozenset` object. Equivalent to
113 ``len(anyset)``. Raises a :exc:`PyExc_SystemError` if *anyset* is not a
114 :class:`set`, :class:`frozenset`, or an instance of a subtype.
115
116
Georg Brandl60203b42010-10-06 10:11:56 +0000117.. c:function:: Py_ssize_t PySet_GET_SIZE(PyObject *anyset)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000118
Georg Brandl60203b42010-10-06 10:11:56 +0000119 Macro form of :c:func:`PySet_Size` without error checking.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000120
121
Georg Brandl60203b42010-10-06 10:11:56 +0000122.. c:function:: int PySet_Contains(PyObject *anyset, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000123
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300124 Return ``1`` if found, ``0`` if not found, and ``-1`` if an error is encountered. Unlike
Georg Brandl54a3faa2008-01-20 09:30:57 +0000125 the Python :meth:`__contains__` method, this function does not automatically
126 convert unhashable sets into temporary frozensets. Raise a :exc:`TypeError` if
127 the *key* is unhashable. Raise :exc:`PyExc_SystemError` if *anyset* is not a
128 :class:`set`, :class:`frozenset`, or an instance of a subtype.
129
Georg Brandl54a3faa2008-01-20 09:30:57 +0000130
Georg Brandl60203b42010-10-06 10:11:56 +0000131.. c:function:: int PySet_Add(PyObject *set, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000132
Georg Brandle6bcc912008-05-12 18:05:20 +0000133 Add *key* to a :class:`set` instance. Also works with :class:`frozenset`
Georg Brandl60203b42010-10-06 10:11:56 +0000134 instances (like :c:func:`PyTuple_SetItem` it can be used to fill-in the values
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300135 of brand new frozensets before they are exposed to other code). Return ``0`` on
136 success or ``-1`` on failure. Raise a :exc:`TypeError` if the *key* is
Georg Brandle6bcc912008-05-12 18:05:20 +0000137 unhashable. Raise a :exc:`MemoryError` if there is no room to grow. Raise a
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300138 :exc:`SystemError` if *set* is not an instance of :class:`set` or its
Georg Brandl54a3faa2008-01-20 09:30:57 +0000139 subtype.
140
Christian Heimesfd66e512008-01-29 12:18:50 +0000141
142The following functions are available for instances of :class:`set` or its
143subtypes but not for instances of :class:`frozenset` or its subtypes.
144
Georg Brandl54a3faa2008-01-20 09:30:57 +0000145
Georg Brandl60203b42010-10-06 10:11:56 +0000146.. c:function:: int PySet_Discard(PyObject *set, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000147
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300148 Return ``1`` if found and removed, ``0`` if not found (no action taken), and ``-1`` if an
Georg Brandl54a3faa2008-01-20 09:30:57 +0000149 error is encountered. Does not raise :exc:`KeyError` for missing keys. Raise a
Serhiy Storchaka0b68a2d2013-10-09 13:26:17 +0300150 :exc:`TypeError` if the *key* is unhashable. Unlike the Python :meth:`~set.discard`
Georg Brandl54a3faa2008-01-20 09:30:57 +0000151 method, this function does not automatically convert unhashable sets into
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300152 temporary frozensets. Raise :exc:`PyExc_SystemError` if *set* is not an
Georg Brandl54a3faa2008-01-20 09:30:57 +0000153 instance of :class:`set` or its subtype.
154
155
Georg Brandl60203b42010-10-06 10:11:56 +0000156.. c:function:: PyObject* PySet_Pop(PyObject *set)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000157
158 Return a new reference to an arbitrary object in the *set*, and removes the
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200159 object from the *set*. Return ``NULL`` on failure. Raise :exc:`KeyError` if the
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300160 set is empty. Raise a :exc:`SystemError` if *set* is not an instance of
Georg Brandl54a3faa2008-01-20 09:30:57 +0000161 :class:`set` or its subtype.
162
163
Georg Brandl60203b42010-10-06 10:11:56 +0000164.. c:function:: int PySet_Clear(PyObject *set)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000165
166 Empty an existing set of all elements.