Comparing objects - is there a maximum object?

Andrew Dalke adalke at mindspring.com
Fri Sep 5 21:48:48 EDT 2003


Martin v. Löwis:
> No, this would not be used. cmp(a,b) == -cmp(b,a)

True.  Unlike + and *, cmp is communitive.

Here's the results of some instrumentation under 2.3.

>>> class Max:
...    def __getattr__(self, name):
...       print "Looking for", name
...       if name == "__cmp__":
...          return lambda other: 1
...       raise AttributeError(name)
...
>>> data = [Max(), 0, 1, 2, 3]
>>> data.sort()
Looking for __gt__
Looking for __coerce__
Looking for __cmp__
Looking for __gt__
Looking for __coerce__
Looking for __cmp__
Looking for __gt__
Looking for __coerce__
Looking for __cmp__
Looking for __gt__
Looking for __coerce__
Looking for __cmp__
>>> random.shuffle(data); data.sort(); data
Looking for __lt__
Looking for __coerce__
Looking for __cmp__
Looking for __repr__
[0, 1, 2, 3, <__main__.Max instance at 0x013D8C88>]

Not an __rcmp__ in the mix.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list