On 12 October 2011 22:12, Ethan Furman firstname.lastname@example.org wrote:
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==b and a[-1]==b[-1])
While I'm agnostic on the question if whether range(0,9,2) and range(0,10,2) are the same, I'd point out that ranges_equal is straightforward to write and says they are equal. But if you're in the camp of saying they are not equal, you appear to have no way of determining that *except* by comparing reprs, as range objects don't seem to expose their start, step and end values as attributes - unless I've missed something.
r = range(0,10,2) dir(r)
['__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
Rather than worrying about supporting equality operators on ranges, I'd suggest exposing the start, step and end attributes and then leaving people who want them to roll their own equality functions.