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()
,和
PyObject_GetIter()
) or the abstract number protocol (including
PyNumber_And()
,
PyNumber_Subtract()
,
PyNumber_Or()
,
PyNumber_Xor()
,
PyNumber_InPlaceAnd()
,
PyNumber_InPlaceSubtract()
,
PyNumber_InPlaceOr()
,和
PyNumber_InPlaceXor()
).
PySetObject
¶
此子类型的
PyObject
is used to hold the internal data for both
set
and
frozenset
objects. It is like a
PyDictObject
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 are subject to change. All access should be done through the documented API rather than by manipulating the values in the structure.
PySet_Type
¶
This is an instance of
PyTypeObject
representing the Python
set
类型。
PyFrozenSet_Type
¶
This is an instance of
PyTypeObject
representing the Python
frozenset
类型。
The following type check macros work on pointers to any Python object. Likewise, the constructor functions work with any iterable Python object.
PyAnySet_Check
(
PyObject
*p
)
¶
返回 True 若
p
是
set
object, a
frozenset
object, or an instance of a subtype.
PyAnySet_CheckExact
(
PyObject
*p
)
¶
返回 True 若
p
是
set
object or a
frozenset
object but not an instance of a subtype.
PyFrozenSet_CheckExact
(
PyObject
*p
)
¶
返回 True 若
p
是
frozenset
object but not an instance of a subtype.
PySet_New
(
PyObject
*iterable
)
¶
返回新
set
containing objects returned by the
iterable
。
iterable
可以是
NULL
to create a new empty set. Return the new set on success or
NULL
on failure. Raise
TypeError
if
iterable
is not actually iterable. The constructor is also useful for copying a set (
c=set(s)
).
PyFrozenSet_New
(
PyObject
*iterable
)
¶
返回新
frozenset
containing objects returned by the
iterable
.
iterable
可以是
NULL
to create a new empty frozenset. Return the new set on success or
NULL
on failure. Raise
TypeError
if
iterable
is not actually iterable.
The following functions and macros are available for instances of
set
or
frozenset
or instances of their subtypes.
PySet_Size
(
PyObject
*anyset
)
¶
Return the length of a
set
or
frozenset
object. Equivalent to
len(anyset)
。引发
PyExc_SystemError
if
anyset
is not a
set
,
frozenset
, or an instance of a subtype.
PySet_GET_SIZE
(
PyObject
*anyset
)
¶
Macro form of
PySet_Size()
without error checking.
PySet_Contains
(
PyObject
*anyset
,
PyObject
*key
)
¶
返回
1
if found,
0
if not found, and
-1
if an error is encountered. Unlike the Python
__contains__()
method, this function does not automatically convert unhashable sets into temporary frozensets. Raise a
TypeError
若
key
is unhashable. Raise
PyExc_SystemError
if
anyset
is not a
set
,
frozenset
, or an instance of a subtype.
PySet_Add
(
PyObject
*set
,
PyObject
*key
)
¶
添加
key
到
set
instance. Also works with
frozenset
instances (like
PyTuple_SetItem()
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
TypeError
若
key
is unhashable. Raise a
MemoryError
if there is no room to grow. Raise a
SystemError
if
set
不是实例化的
set
or its subtype.
The following functions are available for instances of
set
or its subtypes but not for instances of
frozenset
or its subtypes.
PySet_Discard
(
PyObject
*set
,
PyObject
*key
)
¶
返回
1
if found and removed,
0
if not found (no action taken), and
-1
if an error is encountered. Does not raise
KeyError
for missing keys. Raise a
TypeError
若
key
is unhashable. Unlike the Python
discard()
method, this function does not automatically convert unhashable sets into temporary frozensets. Raise
PyExc_SystemError
if
set
不是实例化的
set
or its subtype.
PySet_Pop
(
PyObject
*set
)
¶
Return a new reference to an arbitrary object in the
set
, and removes the object from the
set
。返回
NULL
on failure. Raise
KeyError
if the set is empty. Raise a
SystemError
if
set
不是实例化的
set
or its subtype.
PySet_ClearFreeList
(
)
¶
Clear the free list. Return the total number of freed items.
3.3 版新增。