Objetos Conjunto¶
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()).
-
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 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¶ Esta es una instancia de
PyTypeObjectque representa el tipo Pythonset.
-
PyTypeObject
PyFrozenSet_Type¶ Esta es una instancia de
PyTypeObjectque representa el tipo Pythonfrozenset.
Los siguientes macros de comprobación de tipos funcionan en punteros a cualquier objeto de Python. Del mismo modo, las funciones del constructor funcionan con cualquier objeto Python iterable.
-
int
PySet_Check(PyObject *p)¶ Retorna verdadero si p es un objeto
seto una instancia de un subtipo. Esta función siempre finaliza con éxito.
-
int
PyFrozenSet_Check(PyObject *p)¶ Retorna verdadero si p es un objeto
frozenseto una instancia de un subtipo. Esta función siempre finaliza con éxito.
-
int
PyAnySet_Check(PyObject *p)¶ Retorna verdadero si p es un objeto
set, un objetofrozenset, o una instancia de un subtipo. Esta función siempre finaliza con éxito.
-
int
PyAnySet_CheckExact(PyObject *p)¶ Retorna verdadero si p es un objeto
seto un objetofrozensetpero no una instancia de un subtipo. Esta función siempre finaliza con éxito.
-
int
PyFrozenSet_CheckExact(PyObject *p)¶ Retorna verdadero si p es un objeto
frozensetpero no una instancia de un subtipo. Esta función siempre finaliza con éxito.
-
PyObject*
PySet_New(PyObject *iterable)¶ - Return value: New reference.
Retorna un nuevo
setque contiene objetos retornados por iterable. El iterable puede serNULLpara crear un nuevo conjunto vacío. Retorna el nuevo conjunto en caso de éxito oNULLen caso de error. LanzaTypeErrorsi iterable no es realmente iterable. El constructor también es útil para copiar un conjunto (c=set(s)).
-
PyObject*
PyFrozenSet_New(PyObject *iterable)¶ - Return value: New reference.
Retorna un nuevo
frozensetque contiene objetos retornados por iterable. El iterable puede serNULLpara crear un nuevo conjunto congelado vacío. Retorna el nuevo conjunto en caso de éxito oNULLen caso de error. LanzaTypeErrorsi iterable no es realmente iterable.
Las siguientes funciones y macros están disponibles para instancias de set o frozenset o instancias de sus subtipos.
-
Py_ssize_t
PySet_Size(PyObject *anyset)¶ Retorna la longitud de un objeto
setofrozenset. Equivalente alen(anyset). Lanza unPyExc_SystemErrorsi anyset no esset,frozenset, o una instancia de un subtipo.
-
Py_ssize_t
PySet_GET_SIZE(PyObject *anyset)¶ Forma macro de
PySet_Size()sin comprobación de errores.
-
int
PySet_Contains(PyObject *anyset, PyObject *key)¶ Retorna
1si se encuentra,0si no se encuentra y-1si se encuentra un error. A diferencia del método Python__contains__(), esta función no convierte automáticamente conjuntos no compartibles en congelados temporales. Lanza unTypeErrorsi la key no se puede compartir. LanzaPyExc_SystemErrorsi anyset no es unset,frozenset, o una instancia de un subtipo.
-
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.
Las siguientes funciones están disponibles para instancias de set o sus subtipos, pero no para instancias de frozenset o sus subtipos.
-
int
PySet_Discard(PyObject *set, PyObject *key)¶ Retorna
1si se encuentra y se elimina,0si no se encuentra (no se realiza ninguna acción) y-1si se encuentra un error. No lanzaKeyErrorpor faltar claves. Lanza unTypeErrorsi la key no se puede compartir. A diferencia del método Pythondiscard(), esta función no convierte automáticamente conjuntos no compartibles en congelados temporales. LanzaPyExc_SystemErrorsi set no es una instancia deseto su subtipo.