Set Objects¶
This section details the public API for set and frozenset
objects.  Any functionality not listed below is best accessed using the either
the abstract object protocol (including PyObject_CallMethod(),
PyObject_RichCompareBool(), PyObject_Hash(),
PyObject_Repr(), PyObject_IsTrue(), PyObject_Print(), and
PyObject_GetIter()) or the abstract number protocol (including
PyNumber_And(), PyNumber_Subtract(), PyNumber_Or(),
PyNumber_Xor(), PyNumber_InPlaceAnd(),
PyNumber_InPlaceSubtract(), PyNumber_InPlaceOr(), and
PyNumber_InPlaceXor()).
- 
PySetObject¶
- This subtype of - PyObjectis used to hold the internal data for both- setand- frozensetobjects. It is like a- PyDictObjectin that it is a fixed size for small sets (much like tuple storage) and will point to a separate, variable sized block of memory for medium and large sized sets (much like list storage). None of the fields of this structure should be considered public and are subject to change. All access should be done through the documented API rather than by manipulating the values in the structure.
- 
PyTypeObject PySet_Type¶
- This is an instance of - PyTypeObjectrepresenting the Python- settype.
- 
PyTypeObject PyFrozenSet_Type¶
- This is an instance of - PyTypeObjectrepresenting the Python- frozensettype.
The following type check macros work on pointers to any Python object. Likewise, the constructor functions work with any iterable Python object.
- 
int PySet_Check(PyObject *p)¶
- Return true if p is a - setobject or an instance of a subtype. This function always succeeds.
- 
int PyFrozenSet_Check(PyObject *p)¶
- Return true if p is a - frozensetobject or an instance of a subtype. This function always succeeds.
- 
int PyAnySet_Check(PyObject *p)¶
- Return true if p is a - setobject, a- frozensetobject, or an instance of a subtype. This function always succeeds.
- 
int PyAnySet_CheckExact(PyObject *p)¶
- Return true if p is a - setobject or a- frozensetobject but not an instance of a subtype. This function always succeeds.
- 
int PyFrozenSet_CheckExact(PyObject *p)¶
- Return true if p is a - frozensetobject but not an instance of a subtype. This function always succeeds.
- 
PyObject* PySet_New(PyObject *iterable)¶
- Return value: New reference.Return a new setcontaining objects returned by the iterable. The iterable may beNULLto create a new empty set. Return the new set on success orNULLon failure. RaiseTypeErrorif iterable is not actually iterable. The constructor is also useful for copying a set (c=set(s)).
- 
PyObject* PyFrozenSet_New(PyObject *iterable)¶
- Return value: New reference.Return a new frozensetcontaining objects returned by the iterable. The iterable may beNULLto create a new empty frozenset. Return the new set on success orNULLon failure. RaiseTypeErrorif iterable is not actually iterable.
The following functions and macros are available for instances of set
or frozenset or instances of their subtypes.
- 
Py_ssize_t PySet_Size(PyObject *anyset)¶
- Return the length of a - setor- frozensetobject. Equivalent to- len(anyset). Raises a- PyExc_SystemErrorif anyset is not a- set,- frozenset, or an instance of a subtype.
- 
Py_ssize_t PySet_GET_SIZE(PyObject *anyset)¶
- Macro form of - PySet_Size()without error checking.
- 
int PySet_Contains(PyObject *anyset, PyObject *key)¶
- Return - 1if found,- 0if not found, and- -1if an error is encountered. Unlike the Python- __contains__()method, this function does not automatically convert unhashable sets into temporary frozensets. Raise a- TypeErrorif the key is unhashable. Raise- PyExc_SystemErrorif anyset is not a- set,- frozenset, or an instance of a subtype.
- 
int PySet_Add(PyObject *set, PyObject *key)¶
- Add key to a - setinstance. Also works with- frozensetinstances (like- PyTuple_SetItem()it can be used to fill-in the values of brand new frozensets before they are exposed to other code). Return- 0on success or- -1on failure. Raise a- TypeErrorif the key is unhashable. Raise a- MemoryErrorif there is no room to grow. Raise a- SystemErrorif set is not an instance of- setor its subtype.
The following functions are available for instances of set or its
subtypes but not for instances of frozenset or its subtypes.
- 
int PySet_Discard(PyObject *set, PyObject *key)¶
- Return - 1if found and removed,- 0if not found (no action taken), and- -1if an error is encountered. Does not raise- KeyErrorfor missing keys. Raise a- TypeErrorif the key is unhashable. Unlike the Python- discard()method, this function does not automatically convert unhashable sets into temporary frozensets. Raise- PyExc_SystemErrorif set is not an instance of- setor its subtype.