[issue31254] WeakKeyDictionary/Mapping doesn't call __missing__

Raymond Hettinger report at bugs.python.org
Thu Aug 24 20:31:23 EDT 2017


Raymond Hettinger added the comment:

[Anthony Lee]
> The use case is to generate a mapping of weakly-held objects 
> to unique ids, with something like
> 
> id_map = WeakKeyDictionaryWithMissing(lambda *, _counter=itertools.count(): > next(_counter))

Where are you keeping hard references to the keys?  ISTM, you only have a weak reference, so the object has no hard references.  Entries in the dictionary are discarded when there is no longer a strong reference to the key.

Why did you decide to use a dictionary?  AFAICT, nothing external to the dictionary knows about the keys so there is no way to do lookups.

Overall, it doesn't seem like a WeakKeyDictionary with a __missing__() method is the right approach for this problem.   It makes me question where it makes any sense at all to auto-generate missing keys for a WeakKeyDictionary.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue31254>
_______________________________________


More information about the Python-bugs-list mailing list