Is there any way to work around the limitations of WeakValueDictionary to allow it to hold weak references to built-in types like dict or list? Can something be done at the C level in an extension module? I really need a weakref container that can hold (nearly) any type of object.
+1
A:
According to the Python documentation you can create weak references to subclasses of dict
and list
... it's not a perfect solution, but if you're able to create a custom subclass of dict
and use that instead of a native dict
, it should be good enough. (I've never actually done this myself)
David Zaslavsky
2009-04-03 19:01:08
The problem is that the object to be stored in this container will be coming from other people's code, and the container is an "implementation detail" of a larger set of classes, so I would rather avoid a "must be weak-referencable" kind of requirement.
2009-04-03 19:14:13
ah... well, I don't know of any way to create a fully general WeakValueDictionary that can hold anything at all. There are various tricks you can use in special cases, though - if you edit the question to specify what kinds of objects you need to have weak references to, it might help.
David Zaslavsky
2009-04-03 20:58:04