Are there any list comparison optimizations in Python?

Skip Montanaro skip at pobox.com
Wed Nov 14 03:42:40 EST 2001


    Huaiyu> class A: 
    Huaiyu>    def __cmp__(a,b): return -1

    Huaiyu> a = [A()]*3
    Huaiyu> a == a            # returns 0
    Huaiyu> a is a            # returns 1


    Huaiyu> Is this intended?

Yup, that's what you told the system.  You can override "==" by writing your
own cmp method, but there is no option to override the behvaior of the "is"
operator.  The bytecode generated for "x is y" does nothing more than
compare the two object pointers.  You chose to define __cmp__ such that
nothing is ever equal with anything else, so you have to live with it. ;-)

-- 
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list