[Python-ideas] Adding collections.abc.Ordered

Wes Turner wes.turner at gmail.com
Sat Dec 26 10:02:45 EST 2015


On Nov 8, 2015 1:46 PM, "Guido van Rossum" <guido at python.org> wrote:
>
> I'm warming up slightly to the idea of this ABC.
>
> I've re-read Amir's post, and if I found myself in his situation I would
have used a combination of documenting that __slots__ needs to be ordered
and at runtime only checking for a few common definitely-bad cases. E.g.
for exactly set or dict (not subclasses, since OrderedDict inherits from
dict) would cover most of the common mistakes: most people will use a
literal in their __slots__ definition so we just need to watch out for the
common literals. Those users who are sophisticated enough to use some
advanced mapping type in their __slots__ should just be expected to deal
with the consequences.
>
> But the Ordered ABC, while not essential (unless you're a perfectionist,
in which case you're in the wrong language community anyways :-) still
fills a niche.

I take issue with this comment because it's in the middle of the text.

It's more than a perfection thing:

- Ordered collections may already be Sorted [1]

- [ ] would collectiond.namedtuple [~struct w/ slots] also be Ordered

- [1] here's a rejected PR for jupyter/nbformat
https://github.com/jupyter/nbformat/pull/30 "ENH: v4/nbjson.py:
json.loads(object_pairs_hook=collections.OrderedDict)"


>
> The Ordered ABC should have no additional methods, and no default
implementations. I think it should apply to collections but not to
iterators. It should apply at the level of the read-only interface.
Sequence is always Ordered. Mapping and Set are not by default, but can
have it added. OrderedDict is the prime (maybe only) example -- it's a
MutableMapping and Ordered. We might eventually get an OrderedSet.
>
> A sorted set or mapping (e.g. one implemented using some kind of tree)
should also be considered Ordered, even though otherwise this is a totally
different topic -- while some other languages or branches of math use
"order" to refer to sorting (e.g. "partial ordering"), in Python we make a
distinction: on the one hand there's sorted() and list.sort(), and on the
other hand there's OrderedDict.
>
> So, I think that the Ordered ABC proposed here is totally well-defined
and mildly useful. It may be somewhat confusing (because many people when
they first encounter the term "ordered" they think it's about sorting --
witness some posts in this thread). The use cases are not very important. I
guess I'm -0 on adding it -- if better use cases than Amir's are developed
I might change to +0. (I don't care much about Serhiy's use cases.)
>
> Sorry for the rambling.
>
> --
> --Guido van Rossum (python.org/~guido)
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151226/1fbc2ec5/attachment.html>


More information about the Python-ideas mailing list