[Python-ideas] Implement comparison operators for range objects

Ethan Furman ethan at stoneleaf.us
Wed Oct 12 23:12:28 CEST 2011


Sven Marnach wrote:
> Bruce Leban schrieb am Mi, 12. Okt 2011, um 11:30:17 -0700:
>>    Other than unit testing, what are the use cases? If I was writing a unit
>>    test, I'd be inclined to be very explicit about what I meant
>>    r1 is r2
>>    repr(r1) == repr(r2)
>>    list(r1) == list(r2)
> 
> Even with a useful '==' operator defined, you could still use 'r1 ==
> r2' or 'r1 is r2', depending on the intended semnatics, just as with
> every other data type.  You just wouldn't need to expand the range to
> a list.
> 
> Comparing the representations doesn't ever seem useful, though.

Agreed -- comparing repr()s seems like a horrible way to do it.

As far as comparing for equality, there's an excellent answer on 
StackOverflow -- http://stackoverflow.com/questions/7740796

def ranges_equal(a, b):
   return len(a)==len(b) and (len(a)==0 or a[0]==b[0] and a[-1]==b[-1])

~Ethan~



More information about the Python-ideas mailing list