Set Objects
pair: object; set pair: object; frozenset
This section details the public API for set and frozenset objects. Any functionality not listed below is best accessed using 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).
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 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.
This is an instance of
PyTypeObjectrepresenting the Pythonsettype.
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.
Return true if p is a
setobject or an instance of a subtype. This function always succeeds.
Return true if p is a
frozensetobject or an instance of a subtype. This function always succeeds.
Return true if p is a
setobject, afrozensetobject, or an instance of a subtype. This function always succeeds.
Return true if p is a
setobject but not an instance of a subtype. This function always succeeds.3.10
Return true if p is a
setobject or afrozensetobject but not an instance of a subtype. This function always succeeds.
Return true if p is a
frozensetobject but not an instance of a subtype. This function always succeeds.
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)).[!NOTE] The operation is atomic on
free threading <free-threaded build>when iterable is aset,frozenset,dictorfrozendict.
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.[!NOTE] The operation is atomic on
free threading <free-threaded build>when iterable is aset,frozenset,dictorfrozendict.
The following functions and macros are available for instances of set or frozenset or instances of their subtypes.
pair: built-in function; len
Return the length of a
setorfrozensetobject. Equivalent tolen(anyset). Raises aSystemErrorif anyset is not aset,frozenset, or an instance of a subtype.
Macro form of
PySet_Sizewithout error checking.
Return
1if found,0if not found, and-1if an error is encountered. Unlike the Python~object.__contains__method, this function does not automatically convert unhashable sets into temporary frozensets. Raise aTypeErrorif the key is unhashable. RaiseSystemErrorif anyset is not aset,frozenset, or an instance of a subtype.[!NOTE] The operation is atomic on
free threading <free-threaded build>when key isstr,int,float,boolorbytes.
Add key to a
setinstance. Also works withfrozensetinstances (likePyTuple_SetItemit can be used to fill in the values of brand new frozensets before they are exposed to other code). Return0on success or-1on failure. Raise aTypeErrorif the key is unhashable. Raise aMemoryErrorif there is no room to grow. Raise aSystemErrorif set is not an instance ofsetor its subtype.[!NOTE] The operation is atomic on
free threading <free-threaded build>when key isstr,int,float,boolorbytes.
The following functions are available for instances of set or its subtypes but not for instances of frozenset or its subtypes.
Return
1if found and removed,0if not found (no action taken), and-1if an error is encountered. Does not raiseKeyErrorfor missing keys. Raise aTypeErrorif the key is unhashable. Unlike the Python~set.discardmethod, this function does not automatically convert unhashable sets into temporary frozensets. RaiseSystemErrorif set is not an instance ofsetor its subtype.[!NOTE] The operation is atomic on
free threading <free-threaded build>when key isstr,int,float,boolorbytes.
Return a new reference to an arbitrary object in the set, and removes the object from the set. Return
NULLon failure. RaiseKeyErrorif the set is empty. Raise aSystemErrorif set is not an instance ofsetor its subtype.
Empty an existing set of all elements. Return
0on success. Return-1and raiseSystemErrorif set is not an instance ofsetor its subtype.[!NOTE] In the
free-threaded build, the set is emptied before its entries are cleared, so other threads will observe an empty set rather than intermediate states.
Deprecated API
A constant representing the size of an internal preallocated table inside
PySetObjectinstances.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_MINSIZEcan be replaced with a small constant like8.If looking for the size of a set, use
PySet_Sizeinstead.3.14
谢谢你的反馈!