[prev in list] [next in list] [prev in thread] [next in thread]
List: python-list
Subject: Re: Would there be support for a more general cmp/__cmp__
From: bokr () oz ! net (Bengt Richter)
Date: 2005-10-27 17:45:31
Message-ID: 43610f31.1111721130 () news ! oz ! net
[Download RAW message or body]
On 27 Oct 2005 08:12:15 GMT, Antoon Pardon <apardon@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
--
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic