Are there any list comparison optimizations in Python?

Emile van Sebille emile at fenx.com
Wed Nov 14 10:32:07 EST 2001


"Huaiyu Zhu" <huaiyu at gauss.almadan.ibm.com> wrote in message
news:slrn9v2qc7.1ss.huaiyu at gauss.almadan.ibm.com...
> On 12 Nov 2001 23:45:51 -0500, David Bolen <db3l at fitlinxx.com> wrote:
>
> >On the non-empirical front, I peeked at the listobject.c source for
> >its list_richcompare function and no, there doesn't appear to be any
> >shortcut check for having both lists refer to the same object (at
> >least in the latest CVS source).  It does do the length check (for ==
> >and !=), but otherwise it's the full recursion with the generic
> >comparison applied to each element.
> >
> >It would be an easy optimization to add, although I'm wondering how
> >much benefit typical real-life code would see.
> >
>
> class A:
>    def __cmp__(a,b): return -1
>
> a = [A()]*3
> a == a            # returns 0
> a is a            # returns 1
>
>
> Is this intended?

Yes, as decreed by the author of class A.


 I would think that mandating
>     a is b  implies  a == b
> would make programs easier to read.
>

Because it's in the class that way, you'd have to assume the author knew
what was intended.

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list