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

Masklinn masklinn at masklinn.net
Wed Jan 2 22:13:57 CET 2013


On 2013-01-02, at 21:37 , Mike Meyer wrote:

On Wed, Jan 2, 2013 at 2:34 PM, Serhiy Storchaka <storchaka at gmail.com> wrote:

середа 02 січень 2013 21:43:47 Eli Bendersky ви написали:

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. What interface for the customization is possible? Obviously, a dict constructor can't have a special keyword argument. How about a setkey method? It takes a single callable as an argument. You'd get your behavior with dict.setkey(id). If called when the dict is non-empty, it should throw an exception.

Wouldn't it make more sense to provide e.g. collections.KeyedDictionary(key, seq, **kwargs)? It would be clear and would allow implementations to provide dedicated implementations for special cases (such as key=id) if desired or necessary.

defaultdict already follows this pattern, so there's a precedent.



More information about the Python-ideas mailing list