[Python-3000] Equality of range objects
Steven Bethard
steven.bethard at gmail.com
Tue Apr 8 18:39:03 CEST 2008
On Tue, Apr 8, 2008 at 7:25 AM, Anand Balachandran Pillai
<abpillai at gmail.com> wrote:
> 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 ?
People carrying code from 2.x to 3.0 should be using xrange, not range::
ActivePython 2.5.1.1 (ActiveState Software Inc.) based on
Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) [MSC v.1310 32
bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> xrange(5, 10) == xrange(5, 10)
False
Steve
--
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
--- Bucky Katt, Get Fuzzy
More information about the Python-3000
mailing list