弱參照物件

Python supports weak references as first-class objects. There are two specific object types which directly implement weak references. The first is a simple reference object, and the second acts as a proxy for the original object as much as it can.

int PyWeakref_Check(ob)

Return true if ob is either a reference or proxy object.

int PyWeakref_CheckRef(ob)

Return true if ob is a reference object.

int PyWeakref_CheckProxy(ob)

Return true if ob is a proxy object.

PyObject* PyWeakref_NewRef(PyObject *ob, PyObject *callback)
Return value: New reference.

返回对象 ob 的一个弱引用对象。 该函数总是会返回一个新的引用,但不保证创建一个新的对象;它有可能返回一个现有的引用对象。 第二个形参 callback 为一个可调用对象,它会在 ob 被作为垃圾回收时接收通知;它应该接受一个单独形参,即弱引用对象本身。 callback 也可以为 NoneNULL。 如果 ob 不是一个弱引用对象,或者如果 callback 不是可调用对象,NoneNULL,该函数将返回 NULL 并且引发 TypeError

PyObject* PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
Return value: New reference.

返回对象 ob 的一个弱引用代理对象。 该函数将总是返回一个新的引用,但不保证创建一个新的对象;它有可能返回一个现有的代理对象。 第二个形参 callback 为一个可调用对象,它会在 ob 被作为垃圾回收时接收通知;它应该接受一个单独形参,即弱引用对象本身。 callback 也可以为 NoneNULL。 如果 ob 不是一个弱引用对象,或者如果 callback 不是可调用对象,NoneNULL,该函数将返回 NULL 并且引发 TypeError

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

Return the referenced object from a weak reference, ref. If the referent is no longer live, returns Py_None.

備註

This function returns a borrowed reference to the referenced object. This means that you should always call Py_INCREF() on the object except if you know that it cannot be destroyed while you are still using it.

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

Similar to PyWeakref_GetObject(), but implemented as a macro that does no error checking.