[Python-ideas] Identity dicts and sets
Serhiy Storchaka
storchaka at gmail.com
Thu Jan 3 13:09:21 CET 2013
On 03.01.13 05:37, Nick Coghlan wrote:
> I assumed Serhiy meant OrderedDict.
Sorry to have confused you. I meant "ordinary dict".
> 1. It's more correct for caching. For example, "0 + 0" should give
> "0", while "0.0 + 0.0" should give "0.0". An identity based cache will
> get this right, a value based cache will get it wrong
> (functools.lru_cache actually splits the difference and goes with a
> type+value based cache rather than a simple value based cache)
This is not a use case. Two "0" are same key in CPython, but two "1000"
or two "0.0" are not. There is yet one "wart" (as in any other language
which has identity maps).
> However, one important problem with this kind of data structure is
> that it is *very* easy to get into lifecycle problems if you don't
> store at least a weak reference to a real key (since id's may be
> recycled after an object is destroyed, as shown here:
Of course, identity dict and set should got an ownership on its keys and
values, as all other non-weak collections. Except lookup function they
don't differ from their ordinary counterparts.
> Indeed. As noted elsewhere, we already have a nasty composition
> problem between __missing__, order preservation and weak referencing.
I doubt if all combinations have a sense.
More information about the Python-ideas
mailing list