Re: [Python-ideas] Identity dicts and sets

середа 02 січень 2013 21:43:47 Eli Bendersky ви написали:
What interface for the customization is possible? Obviously, a dict constructor can't have a special keyword argument.

On Wed, Jan 2, 2013 at 2:34 PM, Serhiy Storchaka <storchaka@gmail.com> wrote:
How about a set_key method? It takes a single callable as an argument. You'd get your behavior with dict.set_key(id). If called when the dict is non-empty, it should throw an exception. <mike

On 2013-01-02, at 21:37 , Mike Meyer wrote:
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.

On Wed, Jan 2, 2013 at 1:13 PM, Masklinn <masklinn@masklinn.net> wrote:
I agree collections is the place to put it but that would give us three specialized subclasses of dictionary which cannot be combined. That is, I can have a dictionary with a default, one that is ordered or one that uses a key function but not any combination of those. It would seem better to have something like Haoyi Li suggested: collections.Dictionary(default=None, ordered=False, key=None) --> a dict subclass of course collections.OrderedDictionary and collections.defaultdict would continue to be available as appropriate aliases to collections.Dictionary. --- Bruce Check it out: http://kck.st/YeqGxQ

On Thu, Jan 3, 2013 at 3:50 AM, Serhiy Storchaka <storchaka@gmail.com>wrote:
I agree that all feature combinations may not make sense. I think a default ordered dict would be useful and if other dict variations are created, combinations of them may be useful too. I don't know if identity dicts are useful enough to add, but I think that if another dict variation is added, using a factory should be considered. I have specifically wanted a sorted default dict in the past. (A sorted dict is like an ordered dict but the order is sorted by key not by insertion order. It is simulated by iterating over sorted(dict.keys()). I doubt that sorted dict is common enough to be worth adding, but if it were it would be unfortunate to not have a default variation of it.) --- Bruce Check this out: http://kck.st/YeqGxQ

On Wed, Jan 2, 2013 at 2:34 PM, Serhiy Storchaka <storchaka@gmail.com> wrote:
How about a set_key method? It takes a single callable as an argument. You'd get your behavior with dict.set_key(id). If called when the dict is non-empty, it should throw an exception. <mike

On 2013-01-02, at 21:37 , Mike Meyer wrote:
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.

On Wed, Jan 2, 2013 at 1:13 PM, Masklinn <masklinn@masklinn.net> wrote:
I agree collections is the place to put it but that would give us three specialized subclasses of dictionary which cannot be combined. That is, I can have a dictionary with a default, one that is ordered or one that uses a key function but not any combination of those. It would seem better to have something like Haoyi Li suggested: collections.Dictionary(default=None, ordered=False, key=None) --> a dict subclass of course collections.OrderedDictionary and collections.defaultdict would continue to be available as appropriate aliases to collections.Dictionary. --- Bruce Check it out: http://kck.st/YeqGxQ

On Thu, Jan 3, 2013 at 3:50 AM, Serhiy Storchaka <storchaka@gmail.com>wrote:
I agree that all feature combinations may not make sense. I think a default ordered dict would be useful and if other dict variations are created, combinations of them may be useful too. I don't know if identity dicts are useful enough to add, but I think that if another dict variation is added, using a factory should be considered. I have specifically wanted a sorted default dict in the past. (A sorted dict is like an ordered dict but the order is sorted by key not by insertion order. It is simulated by iterating over sorted(dict.keys()). I doubt that sorted dict is common enough to be worth adding, but if it were it would be unfortunate to not have a default variation of it.) --- Bruce Check this out: http://kck.st/YeqGxQ
participants (6)
-
Bruce Leban
-
Haoyi Li
-
Masklinn
-
Mike Meyer
-
Richard Oudkerk
-
Serhiy Storchaka