Objetos de referência fraca

O Python oferece suporte a referências fracas como objetos de primeira classe. Existem dois tipos de objetos específicos que implementam diretamente referências fracas. O primeiro é um objeto de referência simples, e o segundo atua como um intermediário ao objeto original tanto quanto ele pode.

int PyWeakref_Check(ob)

Retorna verdadeiro se ob for um objeto referência ou um objeto intermediário. Esta função sempre tem sucesso.

int PyWeakref_CheckRef(ob)

Retorna verdadeiro se ob for um objeto referência. Esta função sempre tem sucesso.

int PyWeakref_CheckProxy(ob)

Retorna verdadeiro se ob for um objeto intermediário. Esta função sempre tem sucesso.

PyObject *PyWeakref_NewRef(PyObject *ob, PyObject *callback)
Retorna valor: Nova referência. Part of the Stable ABI.

Return a weak reference object for the object ob. This will always return a new reference, but is not guaranteed to create a new object; an existing reference object may be returned. The second parameter, callback, can be a callable object that receives notification when ob is garbage collected; it should accept a single parameter, which will be the weak reference object itself. callback may also be None or NULL. If ob is not a weakly referencable object, or if callback is not callable, None, or NULL, this will return NULL and raise TypeError.

PyObject *PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
Retorna valor: Nova referência. Part of the Stable ABI.

Return a weak reference proxy object for the object ob. This will always return a new reference, but is not guaranteed to create a new object; an existing proxy object may be returned. The second parameter, callback, can be a callable object that receives notification when ob is garbage collected; it should accept a single parameter, which will be the weak reference object itself. callback may also be None or NULL. If ob is not a weakly referencable object, or if callback is not callable, None, or NULL, this will return NULL and raise TypeError.

PyObject *PyWeakref_GetObject(PyObject *ref)
Retorna valor: Referência emprestada. Part of the Stable ABI.

Retorna o objeto referenciado de uma referência fraca, ref. Se o referente não estiver mais em tempo real, retorna Py_None.

Nota

Esta função retorna uma referência emprestada para o objeto referenciado. Isso significa que você deve sempre chamar Py_INCREF() no objeto, exceto quando ele não puder ser destruído antes do último uso da referência emprestada.

PyObject *PyWeakref_GET_OBJECT(PyObject *ref)
Retorna valor: Referência emprestada.

Similar to PyWeakref_GetObject(), but does no error checking.