[Python-ideas] Should range() == range(0)?

Terry Reedy tjreedy at udel.edu
Mon May 7 04:56:19 CEST 2012


On 5/6/2012 10:20 PM, Terry Reedy wrote:
> On 5/6/2012 8:46 PM, Steven D'Aprano wrote:

>> Are you using Python 2 here? If so, you should be looking at xrange, not
>> range. In Python 3, range objects are equal if their start, stop and
>> step attributes are equal, not if their output values are equal:
>>
>> py> range(0) == range(1,1)
>> False
>> py> range(1, 6, 2) == range(1, 7, 2)
>> False
>
> Python 3.3.0a3 (default, May 1 2012, 16:46:00) [MSC v.1500 64 bit
> (AMD64)] on win32
>>>> range(0) == range(1,1)
> True
>>>> range(1, 6, 2) == range(1, 7, 2)
> True
>
> I remember there being a discussion about this, which Guido was part of,
> that since ranges are sequences, not their source inputs, == should
> reflect what they are, and not how they came to be. If ranges A and B
> are equal, len(A) == len(B), A[i] == B[i], and iter(A) and iter(B)
> produce the same sequence -- and vice versa.

I found the change notice in the library manual.
"Changed in version 3.3: Define ‘==’ and ‘!=’ to compare range objects 
based on the sequence of values they define (instead of comparing based 
on object identity)."

That implies, for instance, "range(1,6,2) != range(1,6,2)" in 3.2, which 
is rather useless. Python slowly improves in many little ways.

-- 
Terry Jan Reedy





More information about the Python-ideas mailing list