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

Cathal Garvey cathalgarvey at cathalgarvey.me
Tue Oct 7 12:02:01 CEST 2014


If you haven't written the code yet, how can you say there's something
missing in Python that needs to be implemented, at significant time and
cost, by the Python developers?

You need to provide your best attempt at implementing this in Python
as-is, and identify where the language's design or gaps in the standard
libraries are creating a problem for you and others that's significant
enough to warrant a change to Python's stdlib. Or, show how solving your
problem would create a language that's better, more exciting, or more
fun for the rest of us.

Right now, your use-case seems really specific, and is likely something
that you can solve just by exhaustively listing the types you want to
consider "ordered" as a global tuple and using that tuple as your
isinstance() check. So, `isinstance(foo, (collections.OrderedDict,
tuple, list))` or whatever. Although, actually it sounds like you need
to know more than whether an object has a defined order, but also
whether it's in the order you expect, so really this calls for a
dedicated checking function that does all the magic you want at once.

On 07/10/14 08:00, Ram Rachum wrote:
> Use case not relevant enough for you?
> 
> On Tue, Oct 7, 2014 at 3:40 AM, Guido van Rossum <guido at python.org> wrote:
> 
>> I think I just lost interest in this thread.
>>
>> On Mon, Oct 6, 2014 at 1:41 PM, Ram Rachum <ram at rachum.com> wrote:
>>
>>> On Mon, Oct 6, 2014 at 8:35 PM, Guido van Rossum <guido at python.org>
>>> wrote:
>>>
>>>> On Mon, Oct 6, 2014 at 1:10 AM, Ram Rachum <ram at rachum.com> wrote:
>>>>
>>>>> Here are a couple:
>>>>>
>>>>>  - I'm making a combinatorics package, and a combination space needs to
>>>>> have a __contains__ method that takes a combination and returns whether
>>>>> it's part of a set. Since a combination, unlike a permutation, has no
>>>>> order, I like to have my combinations be canonicalized in a sorted order.
>>>>> For example, in a combination space of 3 items on range(4), (0, 1, 3) would
>>>>> be a combination in that space, but (3, 1, 0) would not because it's not
>>>>> sorted. (It's a k-permutation but not a combination.) However, if the user
>>>>> does `{0, 1, 3} in comb_space` I still want to return True, regardless of
>>>>> whether the set iterator happens to give these items in order or not.
>>>>>
>>>>
>>>> So how are you writing this code today? In the following case, what's in
>>>> the then or else branch?
>>>>
>>>> if not isinstance(x, collections.Ordered):
>>>>     <what???>
>>>> else:
>>>>     <what???>
>>>>
>>>> Even if you could write this, how would you know that an ordered
>>>> argument is in the *canonical* order?
>>>>
>>>
>>> That part isn't written yet (the package is still work-in-progress), but
>>> I'm not sure what the problem is. I'll have the code look at `x`. If it's
>>> marked as ordered, then I'd iterate on it. If it has the correct items
>>> (meaning the items of the sequence that this is a combination space of) and
>>> the items in x are in the same order as they are in the sequence, and it
>>> has the correct number of items, then we have a match. If we have `not
>>> isinstance(x, collections.Ordered)` then I do the same thing except I
>>> ignore the order of the items. What's the problem?
>>>
>>>
>>>>
>>>>  - For the same package, I'm defining `Tally` and `OrderedTally`
>>>>> classes. (Simliar to `Counter` except without having an identity crisis
>>>>> between being a dict subclass and counter; mine are strictly counters.) In
>>>>> the tests I want to easily see whether the class I'm testing is the ordered
>>>>> one or not, so I'll know to run appropriate tests. (There are also
>>>>> `FrozenTally` and `FrozenOrderedTally` so it's not just one class.) I could
>>>>> set `is_ordered = True` on them or give them some base class, but I think a
>>>>> general `collections.Ordered` abstract base class would be the best
>>>>> solution.
>>>>>
>>>>
>>>> Same question.
>>>>
>>>> --
>>>> --Guido van Rossum (python.org/~guido)
>>>>
>>>
>>>
>>
>>
>> --
>> --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/
> 

-- 
Twitter: @onetruecathal, @formabiolabs
Phone: +353876363185
Blog: http://indiebiotech.com
miniLock.io: JjmYYngs7akLZUjkvFkuYdsZ3PyPHSZRBKNm6qTYKZfAM
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0x988B9099.asc
Type: application/pgp-keys
Size: 6176 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141007/61883a98/attachment.key>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141007/61883a98/attachment.sig>


More information about the Python-ideas mailing list