[Python-ideas] Implement comparison operators for range objects

Terry Reedy tjreedy at udel.edu
Sat Oct 15 04:07:16 CEST 2011


On 10/14/2011 1:23 PM, Guido van Rossum wrote:
> We've been bikeshedding long enough. I propose to do the following to
> range() in Python 3.3:
>
> - add read-only attributes .start, .step, .stop
> - add slicing such that it normalizes .stop to .start + the right
> multiple of .step
> - add __eq__ and __hash__ which compare by .start, .step, .stop

I have sometimes thought that we should unify slice and range objects by 
either adding .__iter__ to slice objects or adding the necessary 
attributes to range objects. The proposal above comes close to doing the 
latter. I presume that slice.__eq__ does what you propose for range. All 
that would be missing from range is the slice.indices method.

Both range and slice objects represent a virtual subseqeunce of ints 
with the same three attributes. We use one to explicitly iterate. We use 
the other to select subsequences with an internal iteration.

If range.stop were allowed to be None, as is slice.stop, we also would 
not need itertools.count, which is the third way we represent a virtual 
stepped subsequence of ints.

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list