集合對(duì)象?

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()).

type PySetObject?

This subtype of 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 all 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?
Part of the Stable ABI.

這是一個(gè) PyTypeObject 實(shí)例,表示 Python set 類型。

PyTypeObject PyFrozenSet_Type?
Part of the Stable ABI.

這是一個(gè) PyTypeObject 實(shí)例,表示 Python frozenset 類型。

下列類型檢查宏適用于指向任意 Python 對(duì)象的指針。 類似地,這些構(gòu)造函數(shù)也適用于任意可迭代的 Python 對(duì)象。

int PySet_Check(PyObject *p)?

如果 p 是一個(gè) set 對(duì)象或者是其子類型的實(shí)例則返回真值。 此函數(shù)總是會(huì)成功執(zhí)行。

int PyFrozenSet_Check(PyObject *p)?

如果 p 是一個(gè) frozenset 對(duì)象或者是其子類型的實(shí)例則返回真值。 此函數(shù)總是會(huì)成功執(zhí)行。

int PyAnySet_Check(PyObject *p)?

如果 p 是一個(gè) set 對(duì)象、frozenset 對(duì)象或者是其子類型的實(shí)例則返回真值。 此函數(shù)總是會(huì)成功執(zhí)行。

int PySet_CheckExact(PyObject *p)?

如果 p 是一個(gè) set 對(duì)象但不是其子類型的實(shí)例則返回真值。 此函數(shù)總是會(huì)成功執(zhí)行。

3.10 新版功能.

int PyAnySet_CheckExact(PyObject *p)?

如果 p 是一個(gè) setfrozenset 對(duì)象但不是其子類型的實(shí)例則返回真值。 此函數(shù)總是會(huì)成功執(zhí)行。

int PyFrozenSet_CheckExact(PyObject *p)?

如果 p 是一個(gè) frozenset 對(duì)象但不是其子類型的實(shí)例則返回真值。 此函數(shù)總是會(huì)成功執(zhí)行。

PyObject *PySet_New(PyObject *iterable)?
Return value: New reference. Part of the Stable ABI.

返回一個(gè)新的 set,其中包含 iterable 所返回的對(duì)象。 iterable 可以為 NULL 表示創(chuàng)建一個(gè)新的空集合。 成功時(shí)返回新的集合,失敗時(shí)返回 NULL。 如果 iterable 實(shí)際上不是可迭代對(duì)象則引發(fā) TypeError。 該構(gòu)造器也適用于拷貝集合 (c=set(s))。

PyObject *PyFrozenSet_New(PyObject *iterable)?
Return value: New reference. Part of the Stable ABI.

返回一個(gè)新的 frozenset,其中包含 iterable 所返回的對(duì)象。 iterable 可以為 NULL 表示創(chuàng)建一個(gè)新的空凍結(jié)集合。 成功時(shí)返回新的凍結(jié)集合,失敗時(shí)返回 NULL。 如果 iterable 實(shí)際上不是可迭代對(duì)象則引發(fā) TypeError。

下列函數(shù)和宏適用于 setfrozenset 的實(shí)例或是其子類型的實(shí)例。

Py_ssize_t PySet_Size(PyObject *anyset)?
Part of the Stable ABI.

返回 setfrozenset 對(duì)象的長(zhǎng)度。 等價(jià)于 len(anyset)。 如果 anyset 不是 set, frozenset 或其子類型的實(shí)例則會(huì)引發(fā) PyExc_SystemError。

Py_ssize_t PySet_GET_SIZE(PyObject *anyset)?

宏版本的 PySet_Size(),不帶錯(cuò)誤檢測(cè)。

int PySet_Contains(PyObject *anyset, PyObject *key)?
Part of the Stable ABI.

如果找到返回 1,如果未找到返回 0,如果遇到錯(cuò)誤則返回 -1。 不同于 Python __contains__() 方法,此函數(shù)不會(huì)自動(dòng)將不可哈希的集合轉(zhuǎn)換為臨時(shí)的凍結(jié)集合。 如果 key 為不可哈希對(duì)象則會(huì)引發(fā) TypeError。 如果 anyset 不是 set, frozenset 或其子類型的實(shí)例則會(huì)引發(fā) PyExc_SystemError。

int PySet_Add(PyObject *set, PyObject *key)?
Part of the Stable ABI.

Add key to a 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 if the key is unhashable. Raise a MemoryError if there is no room to grow. Raise a SystemError if set is not an instance of set or its subtype.

下列函數(shù)適用于 set 或其子類型的實(shí)例,但不可用于 frozenset 或其子類型的實(shí)例。

int PySet_Discard(PyObject *set, PyObject *key)?
Part of the Stable ABI.

如果找到并移除返回 1,如果未找到(無操作)返回 0,如果遇到錯(cuò)誤則返回 -1。 對(duì)于不存在的鍵不會(huì)引發(fā) KeyError。 如果 key 為不可哈希對(duì)象則會(huì)引發(fā) TypeError。 不同于 Python discard() 方法,此函數(shù)不會(huì)自動(dòng)將不可哈希的集合轉(zhuǎn)換為臨時(shí)的凍結(jié)集合。 如果 set 不是 set 或其子類型的實(shí)例則會(huì)引發(fā) PyExc_SystemError

PyObject *PySet_Pop(PyObject *set)?
Return value: New reference. Part of the Stable ABI.

返回 set 中任意對(duì)象的新引用,并從 set 中移除該對(duì)象。 失敗時(shí)返回 NULL。 如果集合為空則會(huì)引發(fā) KeyError。 如果 set 不是 set 或其子類型的實(shí)例則會(huì)引發(fā) SystemError

int PySet_Clear(PyObject *set)?
Part of the Stable ABI.

清空現(xiàn)有字典的所有鍵值對(duì)。