Clearing weakrefs

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()”?

Advertisement

2 thoughts on “Clearing weakrefs

  1. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s