弱引用对象¶
Python 支持 “弱引用” 作为一类对象。具体来说,有两种直接实现弱引用的对象。第一种就是简单的引用对象,第二种尽可能地作用为一个原对象的代理。
- 
int PyWeakref_Check(PyObject *ob)¶
- Return non-zero if ob is either a reference or proxy object. This function always succeeds. 
- 
int PyWeakref_CheckRef(PyObject *ob)¶
- Return non-zero if ob is a reference object. This function always succeeds. 
- 
int PyWeakref_CheckProxy(PyObject *ob)¶
- Return non-zero if ob is a proxy object. This function always succeeds. 
- 
PyObject *PyWeakref_NewRef(PyObject *ob, PyObject *callback)¶
- 返回值:新的引用。 Part of the Stable ABI.返回对象 ob 的一个弱引用对象。 该函数总是会返回一个新引用,但不保证创建一个新的对象;它有可能返回一个现有的引用对象。 第二个形参 callback 可以是一个可调用对象,它会在 ob 被作为垃圾回收时接收通知;它应当接受一个单独形参,即弱引用对象本身。 callback 也可以为 None或NULL。 如果 ob 不是一个弱引用对象,或者如果 callback 不是可调用对象、None或NULL,则该函数将返回NULL并引发TypeError。
- 
PyObject *PyWeakref_NewProxy(PyObject *ob, PyObject *callback)¶
- 返回值:新的引用。 Part of the Stable ABI.返回对象 ob 的一个弱引用代理对象。 该函数将总是返回一个新的引用,但不保证创建一个新的对象;它有可能返回一个现有的代理对象。 第二个形参 callback 可以是一个可调用对象,它会在 ob 被作为垃圾回收时接收通敌;它应当接受一个单独形参,即弱引用对象本身。 callback 也可以为 None或NULL。 如果 ob 不是一个弱引用对象,或者如果 callback 不是可调用对象、None或NULL,则该函数将返回NULL并引发TypeError。
- 
int PyWeakref_GetRef(PyObject *ref, PyObject **pobj)¶
-  Part of the Stable ABI since version 3.13.Get a strong reference to the referenced object from a weak reference, ref, into *pobj. - On success, set *pobj to a new strong reference to the referenced object and return 1. 
- If the reference is dead, set *pobj to - NULLand return 0.
- On error, raise an exception and return -1. 
 在 3.13 版本加入. 
- 
PyObject *PyWeakref_GetObject(PyObject *ref)¶
- 返回值:借入的引用。 Part of the Stable ABI.Return a borrowed reference to the referenced object from a weak reference, ref. If the referent is no longer live, returns Py_None.备注 该函数返回被引用对象的一个 borrowed reference。 这意味着应该总是在该对象上调用 Py_INCREF(),除非是当它在借入引用的最后一次被使用之前无法被销毁的时候。从 3.13 版起不建议使用,将在 3.15 版中移除: Use PyWeakref_GetRef()instead.
- 
PyObject *PyWeakref_GET_OBJECT(PyObject *ref)¶
- 返回值:借入的引用。类似于 PyWeakref_GetObject(),但是不带错误检测。从 3.13 版起不建议使用,将在 3.15 版中移除: Use PyWeakref_GetRef()instead.
- 
void PyObject_ClearWeakRefs(PyObject *object)¶
-  Part of the Stable ABI.此函数将被 tp_dealloc处理句柄调用以清空弱引用。此函数将迭代 object 的弱引用并调用这些引用中可能存在的回调。 它将在尝试了所有回调之后返回。