8 .. sectionauthor:: Raymond D. Hettinger <python@rcn.com>
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
19 the 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`).
29 .. ctype:: PySetObject
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.
40 .. cvar:: PyTypeObject PySet_Type
42 This is an instance of :ctype:`PyTypeObject` representing the Python
46 .. cvar:: PyTypeObject PyFrozenSet_Type
48 This is an instance of :ctype:`PyTypeObject` representing the Python
49 :class:`frozenset` type.
51 The following type check macros work on pointers to any Python object. Likewise,
52 the constructor functions work with any iterable Python object.
55 .. cfunction:: int PyAnySet_Check(PyObject *p)
57 Return true if *p* is a :class:`set` object, a :class:`frozenset` object, or an
58 instance of a subtype.
61 .. cfunction:: int PyAnySet_CheckExact(PyObject *p)
63 Return true if *p* is a :class:`set` object or a :class:`frozenset` object but
64 not an instance of a subtype.
67 .. cfunction:: int PyFrozenSet_CheckExact(PyObject *p)
69 Return true if *p* is a :class:`frozenset` object but not an instance of a
73 .. cfunction:: PyObject* PySet_New(PyObject *iterable)
75 Return a new :class:`set` containing objects returned by the *iterable*. The
76 *iterable* may be *NULL* to create a new empty set. Return the new set on
77 success or *NULL* on failure. Raise :exc:`TypeError` if *iterable* is not
78 actually iterable. The constructor is also useful for copying a set
82 .. cfunction:: PyObject* PyFrozenSet_New(PyObject *iterable)
84 Return a new :class:`frozenset` containing objects returned by the *iterable*.
85 The *iterable* may be *NULL* to create a new empty frozenset. Return the new
86 set on success or *NULL* on failure. Raise :exc:`TypeError` if *iterable* is
87 not actually iterable.
89 The following functions and macros are available for instances of :class:`set`
90 or :class:`frozenset` or instances of their subtypes.
93 .. cfunction:: Py_ssize_t PySet_Size(PyObject *anyset)
95 .. index:: builtin: len
97 Return the length of a :class:`set` or :class:`frozenset` object. Equivalent to
98 ``len(anyset)``. Raises a :exc:`PyExc_SystemError` if *anyset* is not a
99 :class:`set`, :class:`frozenset`, or an instance of a subtype.
102 .. cfunction:: Py_ssize_t PySet_GET_SIZE(PyObject *anyset)
104 Macro form of :cfunc:`PySet_Size` without error checking.
107 .. cfunction:: int PySet_Contains(PyObject *anyset, PyObject *key)
109 Return 1 if found, 0 if not found, and -1 if an error is encountered. Unlike
110 the Python :meth:`__contains__` method, this function does not automatically
111 convert unhashable sets into temporary frozensets. Raise a :exc:`TypeError` if
112 the *key* is unhashable. Raise :exc:`PyExc_SystemError` if *anyset* is not a
113 :class:`set`, :class:`frozenset`, or an instance of a subtype.
115 The following functions are available for instances of :class:`set` or its
116 subtypes but not for instances of :class:`frozenset` or its subtypes.
119 .. cfunction:: int PySet_Add(PyObject *set, PyObject *key)
121 Add *key* to a :class:`set` instance. Does not apply to :class:`frozenset`
122 instances. Return 0 on success or -1 on failure. Raise a :exc:`TypeError` if
123 the *key* is unhashable. Raise a :exc:`MemoryError` if there is no room to grow.
124 Raise a :exc:`SystemError` if *set* is an not an instance of :class:`set` or its
128 .. cfunction:: int PySet_Discard(PyObject *set, PyObject *key)
130 Return 1 if found and removed, 0 if not found (no action taken), and -1 if an
131 error is encountered. Does not raise :exc:`KeyError` for missing keys. Raise a
132 :exc:`TypeError` if the *key* is unhashable. Unlike the Python :meth:`discard`
133 method, this function does not automatically convert unhashable sets into
134 temporary frozensets. Raise :exc:`PyExc_SystemError` if *set* is an not an
135 instance of :class:`set` or its subtype.
138 .. cfunction:: PyObject* PySet_Pop(PyObject *set)
140 Return a new reference to an arbitrary object in the *set*, and removes the
141 object from the *set*. Return *NULL* on failure. Raise :exc:`KeyError` if the
142 set is empty. Raise a :exc:`SystemError` if *set* is an not an instance of
143 :class:`set` or its subtype.
146 .. cfunction:: int PySet_Clear(PyObject *set)
148 Empty an existing set of all elements.