[Python-ideas] Identity dicts and sets (original) (raw)

Antoine Pitrou solipsis at pitrou.net
Wed Jan 2 21:03:48 CET 2013


On Wed, 2 Jan 2013 11:43:47 -0800 Eli Bendersky <eliben at gmail.com> wrote:

On Wed, Jan 2, 2013 at 6:01 AM, Serhiy Storchaka <storchaka at gmail.com>wrote:

> I propose to add new standard collection types: IdentityDict and > IdentitySet. They are almost same as ordinal dict and set, but uses > identity check instead of equality check (and id() or hash(id()) as a > hash). They will be useful for pickling, for implementing sizeof() for > compound types, and for other graph algorithms. > > Of course, they can be implemented using ordinal dicts: > > IdentityDict: key -> value as a dict: id(key) -> (key, value) > IdentitySet as a dict: id(value) -> value > > However implementing them directly in the core has advantages, it consumes > less memory and time, and more comfortable for use from C. IdentityDict and > IdentitySet implementations will share almost all code with implementations > of ordinal dict and set, only lookup function and metainformation will be > different. However dict and set already use a lookup function overloading. > > I agree that the data structures may be useful, but is there no way to some allow the customization of existing data structures instead, without losing performance? It's a shame to have another kind of dict just for this purpose.

The implementation kindof already exists in _pickle.c, IIRC (it's used for the memo dict).

Regards

Antoine.



More information about the Python-ideas mailing list