Weak Reference Objects¶
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)¶
- Επιστρεφόμενη τιμή: New reference. Μέρος τουΣταθερό 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)¶
- Επιστρεφόμενη τιμή: New reference. Μέρος τουΣταθερό 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.
Added in version 3.13.
- PyObject*PyWeakref_GetObject(PyObject*ref)¶
- Επιστρεφόμενη τιμή: Borrowed reference. Μέρος τουΣταθερό 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)¶
- Επιστρεφόμενη τιμή: Borrowed reference.
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.
- intPyWeakref_IsDead(PyObject*ref)¶
Test if the weak referenceref is dead. Returns 1 if the reference isdead, 0 if it is alive, and -1 with an error set ifref is not a weakreference object.
Added in version 3.14.
- 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.Added in version 3.13.