[Python-ideas] Make range objects orderable

Nick Coghlan ncoghlan at gmail.com
Sat Apr 25 06:18:47 CEST 2015


On 25 April 2015 at 00:50, Joao S. O. Bueno <jsbueno at python.org.br> wrote:
> Since it does not mean anything (as in meaningless) to compare ranges without
> specifying a "key", and since ordering functions in Python do allow a
> "key" - it looks
> like this problem is already resolved, whatever the need:
>
>>>> sorted([range(10), range(1,14, 3)], key=len)
> [range(1, 14, 3), range(0, 10)]
>>>> sorted([range(10), range(1,14, 3)], key=max)
> [range(0, 10), range(1, 14, 3)]
>>>> sorted([range(10), range(1,14, 3)], key=min)
> [range(0, 10), range(1, 14, 3)]

ranges() are actually defined as memory-efficient tuples these days
(https://docs.python.org/3/library/stdtypes.html#ranges), and have
supported "==" and "!=" using sequence comparison semantics since 3.3,
so there's a defined natural order for them these days (the ordering
of the equivalent tuples).

That said, I'm still not sure it makes sense to allow ordering them.
The cases I can think of where it might make sense (such as ordering
graph nodes) would all use an actual tuple instead.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list