blob: 0aa6bcecda56d0f669790de47effe297913f1b4d [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
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
15.. versionadded:: 2.5
16
17This section details the public API for :class:`set` and :class:`frozenset`
18objects. Any functionality not listed below is best accessed using the either
19the abstract object protocol (including :cfunc:`PyObject_CallMethod`,
20:cfunc:`PyObject_RichCompareBool`, :cfunc:`PyObject_Hash`,
21:cfunc:`PyObject_Repr`, :cfunc:`PyObject_IsTrue`, :cfunc:`PyObject_Print`, and
22:cfunc:`PyObject_GetIter`) or the abstract number protocol (including
23:cfunc:`PyNumber_And`, :cfunc:`PyNumber_Subtract`, :cfunc:`PyNumber_Or`,
24:cfunc:`PyNumber_Xor`, :cfunc:`PyNumber_InPlaceAnd`,
25:cfunc:`PyNumber_InPlaceSubtract`, :cfunc:`PyNumber_InPlaceOr`, and
26:cfunc:`PyNumber_InPlaceXor`).
27
28
29.. ctype:: PySetObject
30
31 This subtype of :ctype:`PyObject` is used to hold the internal data for both
32 :class:`set` and :class:`frozenset` objects. It is like a :ctype:`PyDictObject`
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
40.. cvar:: PyTypeObject PySet_Type
41
42 This is an instance of :ctype:`PyTypeObject` representing the Python
43 :class:`set` type.
44
45
46.. cvar:: PyTypeObject PyFrozenSet_Type
47
48 This is an instance of :ctype:`PyTypeObject` representing the Python
49 :class:`frozenset` type.
50
51The following type check macros work on pointers to any Python object. Likewise,
52the constructor functions work with any iterable Python object.
53
54
Raymond Hettingere84ada32008-01-28 21:48:07 +000055.. cfunction:: int PySet_Check(PyObject *p)
56
57 Return true if *p* is a :class:`set` object or an instance of a subtype.
58
59 .. versionadded:: 2.6
60
Georg Brandlf6842722008-01-19 22:08:21 +000061.. cfunction:: int PyAnySet_Check(PyObject *p)
62
63 Return true if *p* is a :class:`set` object, a :class:`frozenset` object, or an
64 instance of a subtype.
65
66
67.. cfunction:: int PyAnySet_CheckExact(PyObject *p)
68
69 Return true if *p* is a :class:`set` object or a :class:`frozenset` object but
70 not an instance of a subtype.
71
72
73.. cfunction:: int PyFrozenSet_CheckExact(PyObject *p)
74
75 Return true if *p* is a :class:`frozenset` object but not an instance of a
76 subtype.
77
78
79.. cfunction:: PyObject* PySet_New(PyObject *iterable)
80
81 Return a new :class:`set` containing objects returned by the *iterable*. The
82 *iterable* may be *NULL* to create a new empty set. Return the new set on
83 success or *NULL* on failure. Raise :exc:`TypeError` if *iterable* is not
84 actually iterable. The constructor is also useful for copying a set
85 (``c=set(s)``).
86
87
88.. cfunction:: PyObject* PyFrozenSet_New(PyObject *iterable)
89
90 Return a new :class:`frozenset` containing objects returned by the *iterable*.
91 The *iterable* may be *NULL* to create a new empty frozenset. Return the new
92 set on success or *NULL* on failure. Raise :exc:`TypeError` if *iterable* is
93 not actually iterable.
94
Raymond Hettingerecdcb582008-01-28 20:34:33 +000095 .. versionchanged:: 2.6
96 Now guaranteed to return a brand-new :class:`frozenset`. Formerly,
97 frozensets of zero-length were a singleton. This got in the way of
98 building-up new frozensets with :meth:`PySet_Add`.
99
Georg Brandlf6842722008-01-19 22:08:21 +0000100The following functions and macros are available for instances of :class:`set`
101or :class:`frozenset` or instances of their subtypes.
102
103
104.. cfunction:: Py_ssize_t PySet_Size(PyObject *anyset)
105
106 .. index:: builtin: len
107
108 Return the length of a :class:`set` or :class:`frozenset` object. Equivalent to
109 ``len(anyset)``. Raises a :exc:`PyExc_SystemError` if *anyset* is not a
110 :class:`set`, :class:`frozenset`, or an instance of a subtype.
111
112
113.. cfunction:: Py_ssize_t PySet_GET_SIZE(PyObject *anyset)
114
115 Macro form of :cfunc:`PySet_Size` without error checking.
116
117
118.. cfunction:: int PySet_Contains(PyObject *anyset, PyObject *key)
119
120 Return 1 if found, 0 if not found, and -1 if an error is encountered. Unlike
121 the Python :meth:`__contains__` method, this function does not automatically
122 convert unhashable sets into temporary frozensets. Raise a :exc:`TypeError` if
123 the *key* is unhashable. Raise :exc:`PyExc_SystemError` if *anyset* is not a
124 :class:`set`, :class:`frozenset`, or an instance of a subtype.
125
Georg Brandlf6842722008-01-19 22:08:21 +0000126
127.. cfunction:: int PySet_Add(PyObject *set, PyObject *key)
128
129 Add *key* to a :class:`set` instance. Does not apply to :class:`frozenset`
130 instances. Return 0 on success or -1 on failure. Raise a :exc:`TypeError` if
131 the *key* is unhashable. Raise a :exc:`MemoryError` if there is no room to grow.
132 Raise a :exc:`SystemError` if *set* is an not an instance of :class:`set` or its
133 subtype.
134
Raymond Hettingerecdcb582008-01-28 20:34:33 +0000135 .. versionchanged:: 2.6
136 Now works with instances of :class:`frozenset` or its subtypes.
137 Like :cfunc:`PyTuple_SetItem` in that it can be used to fill-in the
138 values of brand new frozensets before they are exposed to other code.
139
140The following functions are available for instances of :class:`set` or its
141subtypes but not for instances of :class:`frozenset` or its subtypes.
142
Georg Brandlf6842722008-01-19 22:08:21 +0000143
144.. cfunction:: int PySet_Discard(PyObject *set, PyObject *key)
145
146 Return 1 if found and removed, 0 if not found (no action taken), and -1 if an
147 error is encountered. Does not raise :exc:`KeyError` for missing keys. Raise a
148 :exc:`TypeError` if the *key* is unhashable. Unlike the Python :meth:`discard`
149 method, this function does not automatically convert unhashable sets into
150 temporary frozensets. Raise :exc:`PyExc_SystemError` if *set* is an not an
151 instance of :class:`set` or its subtype.
152
153
154.. cfunction:: PyObject* PySet_Pop(PyObject *set)
155
156 Return a new reference to an arbitrary object in the *set*, and removes the
157 object from the *set*. Return *NULL* on failure. Raise :exc:`KeyError` if the
158 set is empty. Raise a :exc:`SystemError` if *set* is an not an instance of
159 :class:`set` or its subtype.
160
161
162.. cfunction:: int PySet_Clear(PyObject *set)
163
164 Empty an existing set of all elements.