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()
).
-
type PySetObject¶
This subtype of
PyObject
is used to hold the internal data for bothset
andfrozenset
objects. It is like aPyDictObject
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.
Essa é uma instância de
PyTypeObject
representando o tipo Pythonset
-
PyTypeObject PyFrozenSet_Type¶
- Part of the Stable ABI.
Esta é uma instância de
PyTypeObject
representando 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
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 objetofrozenset
ou uma instância de um subtipo. Esta função sempre tem sucesso.
-
int PySet_CheckExact(PyObject *p)¶
Retorna verdadeiro se p for um objeto
set
, mas não uma instância de um subtipo. Esta função sempre tem sucesso.Novo na versão 3.10.
-
int PyAnySet_CheckExact(PyObject *p)¶
Retorna verdadeiro se p for um objeto
set
ou 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)¶
- Retorna valor: Nova referência. Part of the Stable ABI.
Retorna uma nova
set
contendo objetos retornados pelo iterável iterable. O iterable pode serNULL
para criar um novo conjunto vazio. Retorna o novo conjunto em caso de sucesso ouNULL
em caso de falha. LevantaTypeError
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)¶
- Retorna valor: Nova referência. Part of the Stable ABI.
Retorna uma nova
frozenset
contendo objetos retornados pelo iterável iterable. O iterable pode serNULL
para criar um novo frozenset vazio. Retorna o novo conjunto em caso de sucesso ouNULL
em caso de falha. LevantaTypeError
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)¶
- Part of the Stable ABI.
Retorna o comprimento de um objeto
set
oufrozenset
. Equivalente alen(anyset)
. Levanta umPyExc_SystemError
se 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)¶
- Part of the Stable ABI.
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 umTypeError
se a key não for hasheável. LevantaPyExc_SystemError
se anyset não é umset
,frozenset
, ou uma instância de um subtipo.
-
int PySet_Add(PyObject *set, PyObject *key)¶
- Part of the Stable ABI.
Add key to a
set
instance. Also works withfrozenset
instances (likePyTuple_SetItem()
it can be used to fill in the values of brand new frozensets before they are exposed to other code). Return0
on success or-1
on failure. Raise aTypeError
if the key is unhashable. Raise aMemoryError
if there is no room to grow. Raise aSystemError
if set is not an instance ofset
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)¶
- Part of the Stable ABI.
Retorna
1
se encontrado e removido,0
se não encontrado (nenhuma ação realizada) e-1
se um erro for encontrado. Não levantaKeyError
para chaves ausentes. Levanta umaTypeError
se 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_SystemError
se set não é uma instância deset
ou seu subtipo.
-
PyObject *PySet_Pop(PyObject *set)¶
- Retorna valor: Nova referência. Part of the Stable ABI.
Retorna uma nova referência a um objeto arbitrário no set e remove o objeto do set. Retorna
NULL
em caso de falha. LevantaKeyError
se o conjunto estiver vazio. Levanta umaSystemError
se set não for uma instância deset
ou seu subtipo.
-
int PySet_Clear(PyObject *set)¶
- Part of the Stable ABI.
Limpa todos os elementos de um conjunto existente