[Python-ideas] Adding collections.abc.Ordered

Neil Girdhar mistersheik at gmail.com
Sat Nov 7 12:19:57 EST 2015


FYI: 1. might not work since the elements in a dict may not be sortable.

On Sat, Nov 7, 2015 at 12:06 PM Serhiy Storchaka <storchaka at gmail.com>
wrote:

> On 07.11.15 00:11, Amir Rachum wrote:
> > I am suggesting the addition of a collections abstract base class called
> > "Ordered". Its meaning is that a collection's iteration order is part of
> > its API. The bulk of this mail describes a use case for this. The reason
> > I believe that such abstract base class is required is that there is no
> > way to test this behavior in a given class. An ordered collection has
> > the exact same interface as an unordered collection (e.g, dict and
> > OrderedDict), other than a _promise_ of the API that the order in which
> > this collection will be iterated has some sort of meaning (In
> > OrderedDict, it is the order in which keys were added to it.)
> >
> >
> > As examples, set, frozenset, dict and defaultdict should *not* be
> > considered as ordered. list, OrderedDict, deque and tuple should be
> > considered ordered.
>
> I just wanted to offer this idea. I have two use cases:
>
> 1. Human-readable output. If the collection is not ordered, it is
> preferable to sort it before output. pprint and testing frameworks will
> benefit from this.
>
> 2. Serialization. If the mapping is not ordered, we can serialize its
> content as dict (that can be more efficient), otherwise we have to
> serialize it as a sequence of key-value pairs.
>
> Actually we need only two functions: test if the collection is ordered,
> and a way to register the class as ordered (or unordered).
>
> _______________________________________________
> 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/
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/B1Pt76OtJi8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151107/abd07cd8/attachment.html>


More information about the Python-ideas mailing list