Objeto Set¶
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¶ Essa é uma instância de
PyTypeObjectrepresentando o tipo Pythonset
-
PyTypeObject
PyFrozenSet_Type¶ Esta é uma instância de
PyTypeObjectrepresentando o tipo Pythonfrozenset.
As macros de verificação de tipo a seguir funcionam em ponteiros para qualquer objeto Python. Da mesma forma, as funções construtoras funcionam com qualquer objeto Python iterável.
-
int
PySet_Check(PyObject *p)¶ Retorna verdadeiro se p for um objeto
setou uma instância de um subtipo. Esta função sempre tem sucesso.
-
int
PyFrozenSet_Check(PyObject *p)¶ Retorna verdadeiro se p for um objeto
frozensetou uma instância de um subtipo. Esta função sempre tem sucesso.
-
int
PyAnySet_Check(PyObject *p)¶ Retorna verdadeiro se p for um objeto
set, um objetofrozensetou uma instância de um subtipo. Esta função sempre tem sucesso.
-
int
PyAnySet_CheckExact(PyObject *p)¶ Retorna verdadeiro se p for um objeto
setou um objetofrozenset, mas não uma instância de um subtipo. Esta função sempre tem sucesso.
-
int
PyFrozenSet_CheckExact(PyObject *p)¶ Retorna verdadeiro se p for um objeto
frozenset, mas não uma instância de um subtipo. Esta função sempre tem sucesso.
-
PyObject*
PySet_New(PyObject *iterable)¶ - Return value: New reference.
Retorna uma nova
setcontendo objetos retornados pelo iterável iterable. O iterable pode serNULLpara criar um novo conjunto vazio. Retorna o novo conjunto em caso de sucesso ouNULLem caso de falha. LevantaTypeErrorse iterable não for realmente iterável. O construtor também é útil para copiar um conjunto (c=set(s)).
-
PyObject*
PyFrozenSet_New(PyObject *iterable)¶ - Return value: New reference.
Retorna uma nova
frozensetcontendo objetos retornados pelo iterável iterable. O iterable pode serNULLpara criar um novo frozenset vazio. Retorna o novo conjunto em caso de sucesso ouNULLem caso de falha. LevantaTypeErrorse iterable não for realmente iterável.
As seguintes funções e macros estão disponíveis para instâncias de set ou frozenset ou instâncias de seus subtipos.
-
Py_ssize_t
PySet_Size(PyObject *anyset)¶ Retorna o comprimento de um objeto
setoufrozenset. Equivalente alen(anyset). Levanta umPyExc_SystemErrorse anyset não for umset,frozenset, ou uma instância de um subtipo.
-
Py_ssize_t
PySet_GET_SIZE(PyObject *anyset)¶ Forma macro de
PySet_Size()sem verificação de erros.
-
int
PySet_Contains(PyObject *anyset, PyObject *key)¶ Retorna
1se encontrado,0se não encontrado, e-1se um erro é encontrado. Ao contrário do método Python__contains__(), esta função não converte automaticamente conjuntos não hasheáveis em frozensets temporários. Levanta umTypeErrorse a key não for hasheável. LevantaPyExc_SystemErrorse anyset não é umset,frozenset, ou uma instância de um 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.
As seguintes funções estão disponíveis para instâncias de set ou seus subtipos, mas não para instâncias de frozenset ou seus subtipos.
-
int
PySet_Discard(PyObject *set, PyObject *key)¶ Retorna
1se encontrado e removido,0se não encontrado (nenhuma ação realizada) e-1se um erro for encontrado. Não levantaKeyErrorpara chaves ausentes. Levanta umaTypeErrorse a key não for hasheável. Ao contrário do método Pythondiscard(), esta função não converte automaticamente conjuntos não hasheáveis em frozensets temporários. LevantaPyExc_SystemErrorse set não é uma instância desetou seu subtipo.
-
PyObject*
PySet_Pop(PyObject *set)¶ - Return value: New reference.
Retorna uma nova referência a um objeto arbitrário no set e remove o objeto do set. Retorna
NULLem caso de falha. LevantaKeyErrorse o conjunto estiver vazio. Levanta umaSystemErrorse set não for uma instância desetou seu subtipo.