Set Objects¶
This section details the public API forset andfrozensetobjects. Any functionality not listed below is best accessed using eitherthe abstract object protocol (includingPyObject_CallMethod(),PyObject_RichCompareBool(),PyObject_Hash(),PyObject_Repr(),PyObject_IsTrue(),PyObject_Print(), andPyObject_GetIter()) or the abstract number protocol (includingPyNumber_And(),PyNumber_Subtract(),PyNumber_Or(),PyNumber_Xor(),PyNumber_InPlaceAnd(),PyNumber_InPlaceSubtract(),PyNumber_InPlaceOr(), andPyNumber_InPlaceXor()).
PySetObject¶This subtype of
PyObjectis used to hold the internal data for bothsetandfrozensetobjects. It is like aPyDictObjectin that it is a fixed size for small sets (much like tuple storage) and willpoint to a separate, variable sized block of memory for medium and large sizedsets (much like list storage). None of the fields of this structure should beconsidered public and all are subject to change. All access should be done throughthe documented API rather than by manipulating the values in the structure.
- PyTypeObject
PySet_Type¶ This is an instance of
PyTypeObjectrepresenting the Pythonsettype.
- PyTypeObject
PyFrozenSet_Type¶ This is an instance of
PyTypeObjectrepresenting the Pythonfrozensettype.
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 ifp is a
setobject or an instance of a subtype.This function always succeeds.
- int
PyFrozenSet_Check(PyObject *p)¶ Return true ifp is a
frozensetobject or an instance of asubtype. This function always succeeds.
- int
PyAnySet_Check(PyObject *p)¶ Return true ifp is a
setobject, afrozensetobject, or aninstance of a subtype. This function always succeeds.
- int
PyAnySet_CheckExact(PyObject *p)¶ Return true ifp is a
setobject or afrozensetobject butnot an instance of a subtype. This function always succeeds.
- int
PyFrozenSet_CheckExact(PyObject *p)¶ Return true ifp is a
frozensetobject but not an instance of asubtype. This function always succeeds.
- PyObject*
PySet_New(PyObject *iterable)¶ - Return value: New reference.
Return a new
setcontaining objects returned by theiterable. Theiterable may beNULLto create a new empty set. Return the new set onsuccess orNULLon failure. RaiseTypeErrorifiterable is notactually 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 theiterable.Theiterable may beNULLto create a new empty frozenset. Return the newset on success orNULLon failure. RaiseTypeErrorifiterable isnot actually iterable.
The following functions and macros are available for instances ofsetorfrozenset or instances of their subtypes.
- Py_ssize_t
PySet_Size(PyObject *anyset)¶ Return the length of a
setorfrozensetobject. Equivalent tolen(anyset). Raises aPyExc_SystemErrorifanyset is not aset,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. Unlikethe Python__contains__()method, this function does not automaticallyconvert unhashable sets into temporary frozensets. Raise aTypeErrorifthekey is unhashable. RaisePyExc_SystemErrorifanyset is not aset,frozenset, or an instance of a subtype.
- int
PySet_Add(PyObject *set,PyObject *key)¶ Addkey to a
setinstance. Also works withfrozensetinstances (likePyTuple_SetItem()it can be used to fill in the valuesof brand new frozensets before they are exposed to other code). Return0onsuccess or-1on failure. Raise aTypeErrorif thekey isunhashable. Raise aMemoryErrorif there is no room to grow. Raise aSystemErrorifset is not an instance ofsetor itssubtype.
The following functions are available for instances ofset or itssubtypes but not for instances offrozenset or its subtypes.
- int
PySet_Discard(PyObject *set,PyObject *key)¶ Return
1if found and removed,0if not found (no action taken), and-1if anerror is encountered. Does not raiseKeyErrorfor missing keys. Raise aTypeErrorif thekey is unhashable. Unlike the Pythondiscard()method, this function does not automatically convert unhashable sets intotemporary frozensets. RaisePyExc_SystemErrorifset is not aninstance ofsetor its subtype.