[Python-ideas] Make range objects orderable

Terry Reedy tjreedy at udel.edu
Fri Apr 24 03:11:32 CEST 2015


On 4/23/2015 8:31 PM, MRAB wrote:
> On 2015-04-24 01:15, Ethan Furman wrote:
>> On 04/23, Riley Banks wrote:
>>
>>> I propose adding the ability to compare range objects using methods
>>> (e.g.
>>> range.issubrange) and/or regular operators. Example:
>>>
>>>     In [56]: range(0, 10, 3).issubrange(range(10))
>>>     Out[56]: True
>>>
>>>     In [57]: range(0, 10, 3) <= range(10)
>>>     Out[57]: True

This was not obvious to me.  Ranges are not sets.

>>>     In [58]: range(10) <= range(0, 10, 3)
>>>     Out[58]: False
>>
>> I seem to recall orderable ranges being rejected before.  For example, is
>> less-than, or subrange, dependent on the values themselves, or on the
>> lowest and highest values, or on the start and end value, or ...

I remember the same.  There is no intuitive answer.  Let us not rehash 
this again.

>> In other words, given:
>>
>>    a = range(11)
>>    b = range(12, step=3)
>>
>> is a < b, or b < a?  Why?
>>
> It could be functionally equivalent to list(a) < list(b).

For range(0, 10, 3) <= range(10), this would give the opposite answer as 
the set interpretation.


-- 
Terry Jan Reedy



More information about the Python-ideas mailing list