sorteddict PEP proposal [started off as orderedict]

Mark Summerfield m.n.summerfield at googlemail.com
Tue Sep 25 13:51:01 CEST 2007


On 25 Sep, 12:19, Paul Hankin <paul.han... at gmail.com> wrote:
> Recall sorted...
>     sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted
> list
>
> So why not construct sorteddicts using the same idea of sortedness?
>
> sorteddict((mapping | sequence | nothing), cmp=None, key=None,
> reverse=None)
>
> Then we can specify the exact behaviour of sorteddicts: it's the same
> as a regular dict, except when the order of keys is important they're
> ordered as if they were sorted by sorted(keys, cmp=sd._cmp,
> key=sd._key, reverse=sd._reverse). Here I imagine cmp, key and reverse
> are stored in the new sorteddict as attributes _cmp, _key, _reverse -
> obviously the actual implementation may differ.
>
> This has the benefit of making sorteddict's behaviour explicit and
> easy to understand, and doesn't introduce a new sorting API when we
> already have a perfectly decent one.
>
> The only problem here is the **kwargs form of the dict constructor
> doesn't translate as we're using keyword args to pass in the sort
> criterion. Perhaps someone has an idea of how this can be solved. If
> nothing else, this constructor could be dropped for sorteddicts, and
> sorteddict(dict(**kwargs), cmp=..., key=..., reverse=...) when that
> behaviour is wanted.
>
> I don't like the integral indexing idea or the slicing: indexing and
> slicing are already part of python and it's bad to have different
> syntax for the same concept on sorteddicts. I'd say it's not an
> important enough for sorteddicts anyway.
>
> --
> Paul Hankin


This makes a lot of sense to me. But how do you envisage it would be
implemented?





More information about the Python-list mailing list