Набір об’єктів¶
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
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 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¶ Це екземпляр
PyTypeObject, що представляє типsetPython.
-
PyTypeObject
PyFrozenSet_Type¶ Це екземпляр
PyTypeObject, що представляє типfrozensetPython.
Наступні макроси перевірки типу працюють з покажчиками на будь-який об’єкт Python. Подібним чином функції конструктора працюють з будь-яким ітерованим об’єктом Python.
-
int
PyFrozenSet_Check(PyObject *p)¶ Return true if p is a
frozensetobject or an instance of a subtype.
-
int
PyAnySet_Check(PyObject *p)¶ Return true if p is a
setobject, afrozensetobject, or an instance of a subtype.
-
int
PyAnySet_CheckExact(PyObject *p)¶ Return true if p is a
setobject or afrozensetobject but not an instance of a subtype.
-
int
PyFrozenSet_CheckExact(PyObject *p)¶ Return true if p is a
frozensetobject but not an instance of a subtype.
-
PyObject*
PySet_New(PyObject *iterable)¶ - Return value: New reference.
Повертає новий
set, що містить об’єкти, повернуті iterable. Iterable може бутиNULLдля створення нового порожнього набору. Повертає новий набір у разі успіху абоNULLу разі невдачі. ВикликатиTypeError, якщо iterable насправді не можна ітерувати. Конструктор також корисний для копіювання набору (c=set(s)).
-
PyObject*
PyFrozenSet_New(PyObject *iterable)¶ - Return value: New reference.
Повертає новий
frozenset, що містить об’єкти, повернуті iterable. Iterable може бутиNULLдля створення нового порожнього замороженого набору. Повертає новий набір у разі успіху абоNULLу разі невдачі. ВикликатиTypeError, якщо iterable насправді не можна ітерувати.
Наступні функції та макроси доступні для екземплярів set або frozenset або екземплярів їхніх підтипів.
-
Py_ssize_t
PySet_Size(PyObject *anyset)¶ Return the length of a
setorfrozensetobject. Equivalent tolen(anyset). Raises aPyExc_SystemErrorif anyset is not aset,frozenset, or an instance of a subtype.
-
Py_ssize_t
PySet_GET_SIZE(PyObject *anyset)¶ Макроформа
PySet_Size()без перевірки помилок.
-
int
PySet_Contains(PyObject *anyset, PyObject *key)¶ Return
1if found,0if not found, and-1if an error is encountered. Unlike the Python__contains__()method, this function does not automatically convert unhashable sets into temporary frozensets. Raise aTypeErrorif the key is unhashable. RaisePyExc_SystemErrorif anyset is not aset,frozenset, or an instance of a subtype.
-
int
PySet_Add(PyObject *set, PyObject *key)¶ Add key to a
setinstance. Also works withfrozensetinstances (likePyTuple_SetItem()it 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.
Наступні функції доступні для екземплярів set або його підтипів, але не для екземплярів frozenset або його підтипів.
-
int
PySet_Discard(PyObject *set, PyObject *key)¶ 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 Pythondiscard()method, this function does not automatically convert unhashable sets into temporary frozensets. RaisePyExc_SystemErrorif set is not an instance ofsetor its subtype.
-
PyObject*
PySet_Pop(PyObject *set)¶ - Return value: New reference.
Повертає нове посилання на довільний об’єкт у set та видаляє об’єкт із set. Повертає
NULLу разі помилки. ВикликатиKeyError, якщо набір порожній. ВикликатиSystemError, якщо set не є екземпляромsetабо його підтипу.
-
int
PySet_ClearFreeList()¶ Clear the free list. Return the total number of freed items.
Нове в версії 3.3.