On Wed, Oct 12, 2011 at 4:48 PM, Paul Moore firstname.lastname@example.org wrote:
On 12 October 2011 22:12, Ethan Furman email@example.com 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.
Unless I misunderstood, Guido is basically saying the same thing (the "exposing" part, that is).
+1 on exposing start, step and end +1 on leaving it at that (unless it turns out to be a common case)
Paul. _______________________________________________ Python-ideas mailing list Pythonfirstname.lastname@example.org http://mail.python.org/mailman/listinfo/python-ideas