Are there any list comparison optimizations in Python?

Steve Holden sholden at holdenweb.com
Wed Nov 14 12:33:32 EST 2001


"Huaiyu Zhu" <huaiyu at gauss.almadan.ibm.com> wrote in ...
[ ... ]
>
> 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.
>
Once you create a class where instances are not equal to themselves then
common sense goes out of the window, and readability is no longer your major
problem. If two lists contain items that compare unequal, then surely by
definition the lists are unequal, and so *not* short-circuiting the
comparison gives correct behavior.

Ask any mathematician, and they will tell you that

    (a != b ) => ({a} != {b})

using the nearest ASCII approximation to set theoretical notation I can
produce. Oddly enough, this makes sense to me.

but-then-sense!=brains-ly y'rs  - steve





More information about the Python-list mailing list