弱引用对象

Python 支持 “弱引用” 作为一类对象。具体来说,有两种直接实现弱引用的对象。第一种就是简单的引用对象,第二种尽可能地作用为一个原对象的代理。

int PyWeakref_Check(ob)

如果 ob 是一个引用或代理对象则返回真值。 此函数总是会成功执行。

int PyWeakref_CheckRef(ob)

如果 ob 是一个引用对象则返回真值。 此函数总是会成功执行。

int PyWeakref_CheckProxy(ob)

如果 ob 是一个代理对象则返回真值。 此函数总是会成功执行。

PyObject *PyWeakref_NewRef(PyObject *ob, PyObject *callback)
Return value: New reference. 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)
Return value: New reference. 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)
Return value: Borrowed reference. Part of the Stable ABI.

返回弱引用对象 ref 的被引用对象。如果被引用对象不再存在,则返回 Py_None

备注

该函数返回被引用对象的一个 borrowed reference。 这意味着应该总是在该对象上调用 Py_INCREF(),除非是当它在借入引用的最后一次被使用之前无法被销毁的时候。

PyObject *PyWeakref_GET_OBJECT(PyObject *ref)
Return value: Borrowed reference.

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