Contagem de Referências

As macros nesta seção são usadas para gerenciar contagens de referências de objetos Python.

void Py_INCREF(PyObject *o)

Increment the reference count for object o.

This function is usually used to convert a borrowed reference to a strong reference in-place. The Py_NewRef() function can be used to create a new strong reference.

The object must not be NULL; if you aren’t sure that it isn’t NULL, use Py_XINCREF().

void Py_XINCREF(PyObject *o)

Aumenta a contagem de referências para o objeto o. O objeto pode ser NULL, caso em que a macro não tem efeito.

See also Py_XNewRef().

PyObject *Py_NewRef(PyObject *o)
Part of the Stable ABI since version 3.10.

Create a new strong reference to an object: increment the reference count of the object o and return the object o.

When the strong reference is no longer needed, Py_DECREF() should be called on it to decrement the object reference count.

The object o must not be NULL; use Py_XNewRef() if o can be NULL.

Por exemplo:

Py_INCREF(obj);
self->attr = obj;

can be written as:

self->attr = Py_NewRef(obj);

See also Py_INCREF().

Novo na versão 3.10.

PyObject *Py_XNewRef(PyObject *o)
Part of the Stable ABI since version 3.10.

Similar to Py_NewRef(), but the object o can be NULL.

If the object o is NULL, the function just returns NULL.

Novo na versão 3.10.

void Py_DECREF(PyObject *o)

Decrement the reference count for object o.

If the reference count reaches zero, the object’s type’s deallocation function (which must not be NULL) is invoked.

This function is usually used to delete a strong reference before exiting its scope.

The object must not be NULL; if you aren’t sure that it isn’t NULL, use Py_XDECREF().

Aviso

A função de desalocação pode fazer com que o código Python arbitrário seja invocado (por exemplo, quando uma instância de classe com um método __del__() é desalocada). Embora as exceções em tal código não sejam propagadas, o código executado tem acesso livre a todas as variáveis globais do Python. Isso significa que qualquer objeto que é alcançável de uma variável global deve estar em um estado consistente antes de Py_DECREF() ser invocado. Por exemplo, o código para excluir um objeto de uma lista deve copiar uma referência ao objeto excluído em uma variável temporária, atualizar a estrutura de dados da lista e então chamar Py_DECREF() para a variável temporária.

void Py_XDECREF(PyObject *o)

Diminui a contagem de referências para o objeto o. O objeto pode ser NULL, caso em que a macro não tem efeito; caso contrário, o efeito é o mesmo de Py_DECREF(), e o mesmo aviso se aplica.

void Py_CLEAR(PyObject *o)

Diminui a contagem de referências para o objeto o. O objeto pode ser NULL, caso em que a macro não tem efeito; caso contrário, o efeito é o mesmo de Py_DECREF(), exceto que o argumento também é definido como NULL. O aviso para Py_DECREF() não se aplica em relação ao objeto passado porque a macro usa cuidadosamente uma variável temporária e define o argumento como NULL antes de diminuir sua contagem de referências.

É uma boa ideia usar essa macro sempre que diminuir a contagem de referências de um objeto que pode ser percorrido durante a coleta de lixo.

void Py_IncRef(PyObject *o)
Part of the Stable ABI.

Increment the reference count for object o. A function version of Py_XINCREF(). It can be used for runtime dynamic embedding of Python.

void Py_DecRef(PyObject *o)
Part of the Stable ABI.

Decrement the reference count for object o. A function version of Py_XDECREF(). It can be used for runtime dynamic embedding of Python.

As seguintes funções ou macros são apenas para uso dentro do núcleo do interpretador: _Py_Dealloc(), _Py_ForgetReference(), _Py_NewReference(), bem como a variável global _Py_RefTotal.