[Python-3000] Equality of range objects
Anand Balachandran Pillai
abpillai at gmail.com
Tue Apr 8 15:25:18 CEST 2008
Hi,
There seems to be inconsistency in the way the new range(...)
type implements equality and inequality operators.
In Python 2.x, range(...) of course returns lists and when you
equate lhs of two range(...) functions over the same range, you
get True, since we are comparing equal lists.
Python 2.5.1 (r251:54863, Sep 6 2007, 17:27:08)
[GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> range(5,10)==range(5,10)
True
>>>
In Py3k, however I see the following behavior.
Python 3.0a4+ (py3k:62126, Apr 3 2008, 16:28:40)
[GCC 4.1.2 20070626 (Red Hat 4.1.2-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> range(5,10)==range(5,10)
False
>>> r1=range(5,10)
>>> r2=range(5,10)
>>> r1==r2
False
>>> r1 != r2
True
Won't this be quite confusing for people who carry forward their
code from 2.x to 3.0 ? Though the range(...) is no longer a function,
but a type, the semantics should not change so much that two range
objects over the same range cannot be equated.
It seems __eq__ is not implemented for range.
>>> r1.__eq__(r2)
NotImplemented
Perhaps this is the problem ?
I could not find much documentation on the range type, so posting
the question here.
Thanks
--Anand
--
-Anand
More information about the Python-3000
mailing list