[Python-3000] ordered dict for p3k collections?
Mark Summerfield
mark at qtrac.eu
Tue Sep 25 10:58:03 CEST 2007
On 2007-09-16, Arvind Singh wrote:
> > How do you get from "some keys can't be ordered" to "it doesn't make
> > sense for Python to have sorteddict or sortedset"? If you want to use
> > keys that can't be ordered, then feel free to continue to use dict.
> > For situations in which ordering is important, that language should
> > support that. When did this become an all or nothing proposition?
> > There's plenty of space for both dict and sorteddict.
>
> Sorry for premature conclusions. All I wanted to do was remind the
> potential problems with any "generic" implementation.
>
> And I did say, when ordering is important, we are left with two choices:
> 1) Sort explicitly (whenever required) and be prepared to handle exceptions
> raised during sort operation.
> 2) Have a implicitly "sorted" implementation and handle exceptions at every
> insertion.
>
> I, personally, tend to prefer the former solution. Later case is useful
> when we have large objects and we do large number of insertions, in which
> case, per insertion exception handling would be inefficient. Former case,
> in turn, can be slightly confusing and a bit to debug.
I can understand your personal preference for dict, although mine is for
sorteddict---but IMO Python should provide both since both are
legitimate in appropriate contexts. To this end I've put a posting on
comp.lang.python with subject:
sorteddict PEP proposal [started off as orderedict]
If there is a positive response I will submit it to the PEP editors. If
there is not, I will just hope that someone else will pick up the idea,
even if in another form or with a different API, because I'd really like
to see some kind of sorted dictionary in Python's standard library. (I
also think there's a similar case for a sorted set.)
--
Mark Summerfield, Qtrac Ltd., www.qtrac.eu
More information about the Python-3000
mailing list