[Python-ideas] Implement comparison operators for range objects

Eric Snow ericsnowcurrently at gmail.com
Thu Oct 13 03:45:01 CEST 2011


On Wed, Oct 12, 2011 at 4:48 PM, Paul Moore <p.f.moore at gmail.com> wrote:
> On 12 October 2011 22:12, Ethan Furman <ethan at stoneleaf.us> 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[0]==b[0] 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)

-eric

>
> Paul.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



More information about the Python-ideas mailing list