Would there be support for a more general cmp/__cmp__

Bengt Richter bokr at oz.net
Thu Oct 27 13:45:31 EDT 2005


On 27 Oct 2005 08:12:15 GMT, Antoon Pardon <apardon at forel.vub.ac.be> wrote:
[...]
>
>The evidence suggests that cmp is not used in sorting. If you have a
>list of sets, sort will happily try to sort it, while calling cmp
>with a set as an argument throws an exception.
>
A data point re evidence:

 >>> class C:
 ...     def __getattr__(self, attr): print attr; raise AttributeError
 ...

 >>> sorted((C(),C()))
 __lt__
 __gt__
 __gt__
 __lt__
 __coerce__
 __coerce__
 __cmp__
 __cmp__
 [__repr__
 <__main__.C instance at 0x02EF388C>, __repr__
 <__main__.C instance at 0x02EF38CC>]

I think it will be slightly different if you define those methods
in a new-style class -- oh, heck, why not do it:

 >>> class D(object):
 ...    def __lt__(*ignore): print '__lt__'; return NotImplemented
 ...    def __gt__(*ignore): print '__gt__'; return NotImplemented
 ...    def __coerce__(*ignore): print '__coerce__'; return NotImplemented
 ...    def __cmp__(*ignore): print '__cmp__'; return NotImplemented
 ...
 >>> sorted((D(),D()))
 __lt__
 __gt__
 __cmp__
 __cmp__

(I haven't followed the thread much, so please excuse if irrelevant ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list