weakref
¶
Weakref utils for compatibility between Python 2 and Python 3 or for extended features.
-
ref
(object, callback=None)[source]¶ Returns a weak reference to object. The original object can be retrieved by calling the reference object if the referent is still alive. If the referent is no longer alive, calling the reference object will cause None to be returned.
The signature is the same as the standard weakref library, but it returns WeakMethod if the object is a bound method.
- Parameters
object – An object
callback (func) – If provided, and the returned weakref object is still alive, the callback will be called when the object is about to be finalized. The weak reference object will be passed as the only parameter to the callback. Then the referent will no longer be available.
- Returns
A weak reference to the object
-
proxy
(object, callback=None)[source]¶ Return a proxy to object which uses a weak reference. This supports use of the proxy in most contexts instead of requiring the explicit dereferencing used with weak reference objects.
The signature is the same as the standard weakref library, but it returns WeakMethodProxy if the object is a bound method.
- Parameters
object – An object
callback (func) – If provided, and the returned weakref object is still alive, the callback will be called when the object is about to be finalized. The weak reference object will be passed as the only parameter to the callback. Then the referent will no longer be available.
- Returns
A proxy to a weak reference of the object
-
class
WeakMethod
(function, callback=None)[source]¶ Wraps a callable object like a function or a bound method. Feature callback when the object is about to be finalized. Provids the same interface as a normal weak reference.
-
class
WeakMethodProxy
(function, callback=None)[source]¶ Wraps a callable object like a function or a bound method with a weakref proxy.