Are there any list comparison optimizations in Python?

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Tue Nov 13 13:42:08 EST 2001


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?   I would think that mandating 
    a is b  implies  a == b
would make programs easier to read.  


Huaiyu



More information about the Python-list mailing list