弱參照物件¶
Python supportsweak references as first-class objects. There are twospecific object types which directly implement weak references. The first is asimple reference object, and the second acts as a proxy for the original objectas much as it can.
- intPyWeakref_Check(PyObject*ob)¶
Return non-zero ifob is either a reference or proxy object. This functionalways succeeds.
- intPyWeakref_CheckRef(PyObject*ob)¶
Return non-zero ifob is a reference object. This function always succeeds.
- intPyWeakref_CheckProxy(PyObject*ob)¶
Return non-zero ifob is a proxy object. This function always succeeds.
- PyObject*PyWeakref_NewRef(PyObject*ob,PyObject*callback)¶
- 回傳值:新的參照。 為穩定 ABI 的一部分.
Return a weak reference object for the objectob. This will always returna new reference, but is not guaranteed to create a new object; an existingreference object may be returned. The second parameter,callback, can be acallable object that receives notification whenob is garbage collected; itshould accept a single parameter, which will be the weak reference objectitself.callback may also be
None
orNULL
. Ifob is not aweakly referenceable object, or ifcallback is not callable,None
, orNULL
, this will returnNULL
and raiseTypeError
.
- PyObject*PyWeakref_NewProxy(PyObject*ob,PyObject*callback)¶
- 回傳值:新的參照。 為穩定 ABI 的一部分.
Return a weak reference proxy object for the objectob. This will alwaysreturn a new reference, but is not guaranteed to create a new object; anexisting proxy object may be returned. The second parameter,callback, canbe a callable object that receives notification whenob is garbagecollected; it should accept a single parameter, which will be the weakreference object itself.callback may also be
None
orNULL
. Ifobis not a weakly referenceable object, or ifcallback is not callable,None
, orNULL
, this will returnNULL
and raiseTypeError
.
- intPyWeakref_GetRef(PyObject*ref,PyObject**pobj)¶
- 為穩定 ABI 的一部分 自 3.13 版本開始.
Get astrong reference to the referenced object from a weakreference,ref, into*pobj.
On success, set*pobj to a newstrong reference to thereferenced object and return 1.
If the reference is dead, set*pobj to
NULL
and return 0.On error, raise an exception and return -1.
在 3.13 版被加入.
- PyObject*PyWeakref_GetObject(PyObject*ref)¶
- 回傳值:借用參照。 為穩定 ABI 的一部分.
Return aborrowed reference to the referenced object from a weakreference,ref. If the referent is no longer live, returns
Py_None
.備註
This function returns aborrowed reference to the referenced object.This means that you should always call
Py_INCREF()
on the objectexcept when it cannot be destroyed before the last usage of the borrowedreference.Deprecated since version 3.13, will be removed in version 3.15:Use
PyWeakref_GetRef()
instead.
- PyObject*PyWeakref_GET_OBJECT(PyObject*ref)¶
- 回傳值:借用參照。
Similar to
PyWeakref_GetObject()
, but does no error checking.Deprecated since version 3.13, will be removed in version 3.15:Use
PyWeakref_GetRef()
instead.
- voidPyObject_ClearWeakRefs(PyObject*object)¶
- 為穩定 ABI 的一部分.
This function is called by the
tp_dealloc
handlerto clear weak references.This iterates through the weak references forobject and calls callbacksfor those references which have one. It returns when all callbacks havebeen attempted.
- voidPyUnstable_Object_ClearWeakRefsNoCallbacks(PyObject*object)¶
- 這是不穩定 API,它可能在小版本發布中沒有任何警告地被變更。
Clears the weakrefs forobject without calling the callbacks.
This function is called by the
tp_dealloc
handlerfor types with finalizers (i.e.,__del__()
). The handler forthose objects first callsPyObject_ClearWeakRefs()
to clear weakrefsand call their callbacks, then the finalizer, and finally this function toclear any weakrefs that may have been created by the finalizer.In most circumstances, it's more appropriate to use
PyObject_ClearWeakRefs()
to clear weakrefs instead of this function.在 3.13 版被加入.