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

Steven D'Aprano steve at pearwood.info
Wed Oct 8 02:14:43 CEST 2014


On Tue, Oct 07, 2014 at 11:27:50PM +0300, Ram Rachum wrote:
[...]
> I meant that my combinatorics package, when asked whether a
> certain iterable belongs in a combination space, would check its order only
> if the iterable type is `Ordered`. (i.e. it's communicating "my order is
> meaningful".)
> 
> For example, someone could have a combination space "3 items taken from
> range(5)", which has members like (0, 1, 2) and (1, 3, 4), but not (4, 2,
> 1) because it's not in canoncial order. If someone were to check `(4, 2, 1)
> in comb_space` he should get `False`, but if he's checking `{1, 2, 4} in
> comb_space` he should get `True`, regardless of iteration order, because
> it's a `set` which isn't supposed to be ordered so it's assumed the user is
> not asking a question about order.

If I have understood you correctly, you have a single function which 
does two different tests, an ordered test and an unordered test, with 
the decision of which to perform being implicitly decided by the input 
type.

I think the Zen of Python has a few things to say about that sort of 
API:

Explicit is better than implicit.
In the face of ambiguity, refuse the temptation to guess.

It sounds to me that you would be better served by two functions, one 
which checks order and one which does not. But if you insist on having a 
single function which decides which test to perform based on the type of 
the input argument, you are under no obligation to attempt to support 
every imaginable sequence type "correctly". You can offer a firm 
guarantee that your function will guess correctly if the type is a 
list, tuple, set or frozenset, and for everything else you offer a "best 
effort" guess as to whether the input sequence is ordered or not.

-- 
Steven


More information about the Python-ideas mailing list