[Python-ideas] issubclass(collections.OrderedDict, collections.Sequence)

Guido van Rossum guido at python.org
Sun Oct 5 23:53:35 CEST 2014


Can I ask you to mathematically define the property you are looking for
here? In some other languages "Ordered" refers to being sorted or sortable,
but the use of this word in OrderedDict does not refer to that at all.

I tried coming up with a definition, but I got stuck with the rather vague
"insertion order is preserved".

Unfortunately, for the other collection types that (presumably) would
qualify for this predicate, like list and tuple, the "insertion" operation
is spelled differently than for OrderedDict -- OrderedDict's rule seems to
be "new elements inserted with __setitem__ are appended to the end of the
underlying sequence", while for list you'd have to write something about
the insert() and append() methods (plus other operations like slice
assignment and extend() that can be reduced to these), and for tuple you'd
have to refer to the constructor.

I don't think that definition is sufficiently crisp and reusable to warrant
defining a collection ABC for it. But perhaps you can come up with a better
specification for your proposed collections.Ordered ABC?

On Sun, Oct 5, 2014 at 1:56 PM, Ram Rachum <ram at rachum.com> wrote:

> Ok, makes sense. Though it still would be nice to have a general way to
> check whether a collection is ordered or not, regardless of index access.
> Perhaps a `collections.Ordered`?
>
> On Sun, Oct 5, 2014 at 11:53 PM, Guido van Rossum <guido at python.org>
> wrote:
>
>> On Sun, Oct 5, 2014 at 11:40 AM, Ram Rachum <ram at rachum.com> wrote:
>>
>>> I was just going to test whether a mapping is dict-like or an
>>> OrderedDict-like object by doing this:
>>>
>>>     isinstance(x, collections.Sequence)
>>>
>>> But then I checked and saw that this is False:
>>>
>>>     issubclass(collections.OrderedDict, collections.Sequence) is False
>>>
>>> Why isn't an OrderedDict a Sequence?
>>>
>>
>> Check the interface of Sequence. It requires indexing behavior that
>> OrdedDict doesn't have -- a[i] is a key lookup in the underlying dict, not
>> an indexed lookup in the ordered list of elements. I don't think it would
>> be wise to attempt to add that behavior either (given that the keys may
>> well be integers).
>>
>> --
>> --Guido van Rossum (python.org/~guido)
>>
>
>


-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141005/09353396/attachment.html>


More information about the Python-ideas mailing list