[Python-ideas] Implement comparison operators for range objects

Ethan Furman ethan at stoneleaf.us
Fri Oct 14 22:10:59 CEST 2011


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

+1

> - add slicing such that it normalizes .stop to .start + the right
> multiple of .step

Already in place.

> - add __eq__ and __hash__ which compare by .start, .step, .stop

-1

--> lst1 = [x for x in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] if x % 3 == 0]
--> lst2 = [x for x in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] if x % 3 == 0]
--> lst3 = [x for x in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] if x % 3
             == 0]
--> lst1
[0, 3, 6, 9]
--> lst2
[0, 3, 6, 9]
--> lst3
[0, 3, 6, 9]
--> lst1 == lst2 == lst3
True

A range is a sequence -- why should identical sequences not compare 
equal?  If you have a case where the start, stop, and step do make a 
difference then that should be the special case where you write your own 
custom code.

Mike Graham wrote:
 > For equality and comparison, this should be the standard. range
 > objects are sequences, and they should compare just like other
 > sequences. If implemented at all, equality should be that they have
 > the same items in the same order.

+1

~Ethan~



More information about the Python-ideas mailing list