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 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

Essa é uma instância de PyTypeObject representando o tipo Python set

PyTypeObject PyFrozenSet_Type

Esta é uma instância de PyTypeObject representando o tipo Python frozenset.

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 set ou uma instância de um subtipo. Esta função sempre tem sucesso.

int PyFrozenSet_Check(PyObject *p)

Retorna verdadeiro se p for um objeto frozenset ou 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 objeto frozenset ou uma instância de um subtipo. Esta função sempre tem sucesso.

int PyAnySet_CheckExact(PyObject *p)

Retorna verdadeiro se p for um objeto set ou um objeto frozenset, 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 set contendo objetos retornados pelo iterável iterable. O iterable pode ser NULL para criar um novo conjunto vazio. Retorna o novo conjunto em caso de sucesso ou NULL em caso de falha. Levanta TypeError se 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 frozenset contendo objetos retornados pelo iterável iterable. O iterable pode ser NULL para criar um novo frozenset vazio. Retorna o novo conjunto em caso de sucesso ou NULL em caso de falha. Levanta TypeError se 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 set ou frozenset. Equivalente a len(anyset). Levanta um PyExc_SystemError se anyset não for um set, 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 1 se encontrado, 0 se não encontrado, e -1 se 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 um TypeError se a key não for hasheável. Levanta PyExc_SystemError se anyset não é um set, frozenset, ou uma instância de um subtipo.

int PySet_Add(PyObject *set, PyObject *key)

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.

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 1 se encontrado e removido, 0 se não encontrado (nenhuma ação realizada) e -1 se um erro for encontrado. Não levanta KeyError para chaves ausentes. Levanta uma TypeError se a key não for hasheável. Ao contrário do método Python discard(), esta função não converte automaticamente conjuntos não hasheáveis em frozensets temporários. Levanta PyExc_SystemError se set não é uma instância de set ou 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 NULL em caso de falha. Levanta KeyError se o conjunto estiver vazio. Levanta uma SystemError se set não for uma instância de set ou seu subtipo.

int PySet_Clear(PyObject *set)

Limpa todos os elementos de um conjunto existente