[Python-ideas] Adding collections.abc.Ordered
Serhiy Storchaka
storchaka at gmail.com
Sat Nov 7 12:06:03 EST 2015
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).
More information about the Python-ideas
mailing list