Set Objects - Python 2.7.18 documentation 编辑
New in version 2.5.
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
PyObject
is used to hold the internal data for bothset
andfrozenset
objects. It is like aPyDictObject
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.
- PyTypeObject
PySet_Type
This is an instance of
PyTypeObject
representing the Pythonset
type.
- PyTypeObject
PyFrozenSet_Type
This is an instance of
PyTypeObject
representing the Pythonfrozenset
type.
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
set
object or an instance of a subtype.New in version 2.6.
- int
PyFrozenSet_Check
(PyObject *p) Return true if p is a
frozenset
object or an instance of a subtype.New in version 2.6.
- int
PyAnySet_Check
(PyObject *p) Return true if p is a
set
object, afrozenset
object, or an instance of a subtype.
- int
PyAnySet_CheckExact
(PyObject *p) Return true if p is a
set
object or afrozenset
object but not an instance of a subtype.
- int
PyFrozenSet_CheckExact
(PyObject *p) Return true if p is a
frozenset
object but not an instance of a subtype.
- PyObject*
PySet_New
(PyObject *iterable) - Return value: New reference.
Return a new
set
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. RaiseTypeError
if 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
frozenset
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. RaiseTypeError
if iterable is not actually iterable.Changed in version 2.6: Now guaranteed to return a brand-new
frozenset
. Formerly, frozensets of zero-length were a singleton. This got in the way of building-up new frozensets withPySet_Add()
.
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
set
orfrozenset
object. Equivalent tolen(anyset)
. Raises aPyExc_SystemError
if anyset is not aset
,frozenset
, or an instance of a subtype.Changed in version 2.5: This function returned an
int
. This might require changes in your code for properly supporting 64-bit systems.
- Py_ssize_t
PySet_GET_SIZE
(PyObject *anyset) Macro form of
PySet_Size()
without error checking.
- int
PySet_Contains
(PyObject *anyset, PyObject *key) Return
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 aTypeError
if the key is unhashable. RaisePyExc_SystemError
if anyset is not aset
,frozenset
, or an instance of a subtype.
- int
PySet_Add
(PyObject *set, PyObject *key) Add key to a
set
instance. Does not apply tofrozenset
instances. Return0
on success or-1
on failure. Raise aTypeError
if the key is unhashable. Raise aMemoryError
if there is no room to grow. Raise aSystemError
if set is not an instance ofset
or its subtype.Changed in version 2.6: Now works with instances of
frozenset
or its subtypes. LikePyTuple_SetItem()
in that it can be used to fill-in the values of brand new frozensets before they are exposed to other code.
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
1
if found and removed,0
if not found (no action taken), and-1
if an error is encountered. Does not raiseKeyError
for missing keys. Raise aTypeError
if the key is unhashable. Unlike the Pythondiscard()
method, this function does not automatically convert unhashable sets into temporary frozensets. RaisePyExc_SystemError
if set is not an instance ofset
or its subtype.
- PyObject*
PySet_Pop
(PyObject *set) - Return value: New reference.
Return a new reference to an arbitrary object in the set, and removes the object from the set. Return NULL on failure. Raise
KeyError
if the set is empty. Raise aSystemError
if set is not an instance ofset
or its subtype.
- int
PySet_Clear
(PyObject *set) Empty an existing set of all elements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论