weak reference callback

Paul Pogonyshev pogonyshev at gmx.net
Sat Aug 29 22:27:03 CEST 2009


Hi,

Is weak reference callback called immediately after the referenced
object is deleted or at arbitrary point in time after that?  I.e. is
it possible to see a dead reference before the callback is called?

More formally, will this ever raise?

    callback_called = False
    def note_deletion (ref):
        callback_called = True

    x = ...
    ref = weakref.ref (x, note_deletion)

    ...

    if ref () is None and not callback_called:
        raise RuntimeError ("reference is dead, yet callback hasn't been called yet")

Paul



More information about the Python-list mailing list