Surprise: del weakref.WeakKeyDictionary[ item ] raises RuntimeError

Mike C. Fletcher mcfletch at rogers.com
Fri Apr 11 11:26:18 EDT 2003


Greg Ewing (using news.cis.dfn.de) wrote:

> Mike C. Fletcher wrote:
>
>>        for ref in self.data.iterkeys():
>>            o = ref()
>>            if o == key:
>>                del self.data[ref]
>>                return
>>
>> Using keys seems to avoid the problem, but with the obvious 
>> disadvantages of keys vs. iterkeys.
>
>
> Maybe a separate list of keys could be maintained
> alongside the dict? 


It would have a pretty significant performance penalty if you always 
maintained a list.  Although I guess that doing two dictionaries:

    ref : object
    id : ref

And then doing all the logic to make sure they keep in synch would give 
you acceptable performance (and much faster than the current version for 
del (at the expense of lots of memory used and slower performance 
elsewhere)).

An "atomic" threading mechanism applied to the iterator-based del method 
might work just as well, I suppose.

In the meantime, using keys() *is* actually "maintaining" the seperate 
key-list for as long as it's needed.

Oh well, has to be some downside to iterators or we wouldn't have any 
fun :) ,
Mike

_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/








More information about the Python-list mailing list