弱参照オブジェクト¶
Python は 弱参照 を第一級オブジェクト (first-class object) としてサポートします。弱参照を直接実装する二種類の固有のオブジェクト型があります。第一は単純な参照オブジェクトで、第二はオリジナルのオブジェクトに対して可能な限りプロキシとして振舞うオブジェクトです。
-
int PyWeakref_CheckRef(PyObject *ob)¶
Return non-zero if ob is a reference object or a subclass of the reference type. This function always succeeds.
-
int PyWeakref_CheckRefExact(PyObject *ob)¶
Return non-zero if ob is a reference object, but not a subclass of the reference type. This function always succeeds.
-
PyObject *PyWeakref_NewRef(PyObject *ob, PyObject *callback)¶
- 戻り値: 新しい参照。 次に属します: 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
NoneorNULL. If ob is not a weakly referenceable object, this will raiseTypeErrorand returnNULL.参考
PyType_SUPPORTS_WEAKREFS()for checking if ob is weakly referenceable.
-
PyObject *PyWeakref_NewProxy(PyObject *ob, PyObject *callback)¶
- 戻り値: 新しい参照。 次に属します: 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
NoneorNULL. If ob weakly referenceable object, this will raiseTypeErrorand returnNULL.参考
PyType_SUPPORTS_WEAKREFS()for checking if ob is weakly referenceable.
-
int PyWeakref_GetRef(PyObject *ref, PyObject **pobj)¶
- 次に属します: Stable ABI (バージョン 3.13 より).
弱参照
refから参照先のオブジェクトへの 強参照 を取得し、*pobjに格納します。成功した場合は、参照先のオブジェクトへの新しい 強参照 を
*pobjに設定し、1 を返します。参照先が消滅している場合は、
*pobjをNULLに設定し、0 を返します。エラーの場合は例外を送出し、-1 を返します。
Added in version 3.13.
-
PyObject *PyWeakref_GetObject(PyObject *ref)¶
- 戻り値: 借用参照。 次に属します: 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.注釈
This function returns a borrowed reference to the referenced object. This means that you should always call
Py_INCREF()on the object except when it cannot be destroyed before the last usage of the borrowed reference.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.
-
void PyObject_ClearWeakRefs(PyObject *object)¶
- 次に属します: Stable ABI.
この関数は、弱参照をクリアするために
tp_deallocハンドラから呼び出されます。objectへの弱参照を順に調べ、コールバックを持つものについてはそのコールバックを呼び出します。 すべてのコールバックの呼び出しを試みた後で戻ります。
-
void PyUnstable_Object_ClearWeakRefsNoCallbacks(PyObject *object)¶
- これは Unstable APIです。マイナーリリースで予告なく変更されることがあります。
コールバックを呼び出さずに
objectへの弱参照をクリアします。この関数は、ファイナライザ (つまり
__del__()) を持つ型のtp_deallocハンドラから呼び出されます。 このようなオブジェクトのハンドラは、まずPyObject_ClearWeakRefs()を呼び出して弱参照をクリアし、そのコールバックを呼び出します。 次にファイナライザを呼び出し、最後にこの関数を呼び出してファイナライザが作成した可能性のある弱参照をクリアします。ほとんどの場合、弱参照のクリアにはこの関数よりも
PyObject_ClearWeakRefs()を使う方が適切です。Added in version 3.13.