This section details the public API for
and
objects. Any functionality not listed below is best accessed using either the abstract object protocol (including
,
,
,
,
,
, and
) or the abstract number protocol (including
,
,
,
,
,
,
, and
).
typePySetObject
This subtype of
is used to hold the internal data for both
and
objects. It is like a
in 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 all are subject to change. All access should be done through the documented API rather than by manipulating the values in the structure.
PySet_Type
Part of the
.This is an instance of
representing the Python
type.
PyFrozenSet_Type
Part of the
.This is an instance of
representing the Python
type.
The following type check macros work on pointers to any Python object. Likewise, the constructor functions work with any iterable Python object.
intPySet_Check(
*p)
Return true if p is a
object or an instance of a subtype. This function always succeeds.
intPyFrozenSet_Check(
*p)
Return true if p is a
object or an instance of a subtype. This function always succeeds.
intPyAnySet_Check(
*p)
Return true if p is a
object, a
object, or an instance of a subtype. This function always succeeds.
intPySet_CheckExact(
*p)
Return true if p is a
object but not an instance of a subtype. This function always succeeds.
Added in version 3.10.
intPyAnySet_CheckExact(
*p)
Return true if p is a
object or a
object but not an instance of a subtype. This function always succeeds.
intPyFrozenSet_CheckExact(
*p)
Return true if p is a
object but not an instance of a subtype. This function always succeeds.
*PySet_New(
*iterable)
Return value: New reference. Part of the
. Thread safety:
Safe for concurrent use on the same object
.Return a new
containing objects returned by the iterable. The iterable may be NULL to create a new empty set. Return the new set on success or NULL on failure. Raise
if iterable is not actually iterable. The constructor is also useful for copying a set (c=set(s)).
*PyFrozenSet_New(
*iterable)
Return value: New reference. Part of the
. Thread safety:
Safe for concurrent use on the same object
.Return a new
containing objects returned by the iterable. The iterable may be NULL to create a new empty frozenset. Return the new set on success or NULL on failure. Raise
if iterable is not actually iterable.
The following functions and macros are available for instances of
or
or instances of their subtypes.
PySet_Size(
*anyset)
Part of the
. Thread safety:
.Return the length of a
or
object. Equivalent to len(anyset). Raises a
if anyset is not a set, frozenset, or an instance of a subtype.
PySet_GET_SIZE(
*anyset)
Thread safety:
.Macro form of
without error checking.
intPySet_Contains(
*anyset,
*key)
Part of the
. Thread safety:
Safe for concurrent use on the same object
.Return 1 if found, 0 if not found, and -1 if an error is encountered. Unlike the Python
method, this function does not automatically convert unhashable sets into temporary frozensets. Raise a
if the key is unhashable. Raise
if anyset is not a
,
, or an instance of a subtype.
intPySet_Add(
*set,
*key)
Part of the
. Thread safety:
Safe for concurrent use on the same object
.Add key to a
instance. Also works with
instances (like
it can be used to fill in the values of brand new frozensets before they are exposed to other code). Return 0 on success or -1 on failure. Raise a
if the key is unhashable. Raise a
if there is no room to grow. Raise a
if set is not an instance of set or its subtype.
The following functions are available for instances of
or its subtypes but not for instances of
or its subtypes.
intPySet_Discard(
*set,
*key)
Part of the
. Thread safety:
Safe for concurrent use on the same object
.Return 1 if found and removed, 0 if not found (no action taken), and -1 if an error is encountered. Does not raise
for missing keys. Raise a
if the key is unhashable. Unlike the Python
method, this function does not automatically convert unhashable sets into temporary frozensets. Raise
if set is not an instance of
or its subtype.
*PySet_Pop(
*set)
Return value: New reference. Part of the
. Thread safety:
.Return a new reference to an arbitrary object in the set, and removes the object from the set. Return NULL on failure. Raise
if the set is empty. Raise a
if set is not an instance of
or its subtype.
intPySet_Clear(
*set)
Part of the
. Thread safety:
.Empty an existing set of all elements. Return 0 on success. Return -1 and raise
if set is not an instance of
or its subtype.
Note
In the
, the set is emptied before its entries are cleared, so other threads will observe an empty set rather than intermediate states.
Deprecated API
PySet_MINSIZE
A constant representing the size of an internal preallocated table inside
instances.
This is documented solely for completeness, as there are no guarantees that a given version of CPython uses preallocated tables with a fixed size. In code that does not deal with unstable set internals, PySet_MINSIZE can be replaced with a small constant like 8.
If looking for the size of a set, use
instead.