I just had this problem which would have been elegantly solved with the ability to manually clear weak references pointing to an object. I am (for technical reasons) recycling an object, so instead of killing it and re-creating it, I re-initialize it. But that leaves old weak references in place. How nice wouldn’t it be to be able to call “myobject.clear_weakrefs()”?
2 thoughts on “Clearing weakrefs”
Are you creating the refs yourself? In that case you could just subclass weakref.ref to make it register at the object it refers to, and a way to dereference it.
Yes, I can do that, but in a large application, written by many it can be messy.
The thing is, the api already has the function PyObject_ClearWeakRefs(). It only needs exposing to python.
It is an object function, so a method on “object” would seem the most straightforward, but a weakref.clear(object) function might also be sensible.
It’s a weird omission.