Guido rethinking removal of cmp from sort method

Aahz aahz at
Fri Apr 8 15:40:18 EDT 2011

In article <4d9f32a2$1 at>,
Lie Ryan  <lie.1296 at> wrote:
>On 04/09/11 01:08, Aahz wrote:
>> Actually, my take is that removing __cmp__ was a mistake.  (I already
>> argued about it back in python-dev before it happened, and I see little
>> point rehashing it.  My reason is strictly efficiency grounds: when
>> comparisons are expensive -- such as Decimal object -- __cmp__ is
>> faster.)
>I don't get you... why would sorting a list using __cmp__ be faster when
>comparisons are expensive?

Not sorting (because sorting only requires one comparison), but any
operation involving multiple comparisons.  Consider this:

if a < b:
elif a == b:

For a >= b, you need to make two comparisons.  Now consider this:

r = cmp(a, b)
if r < 0:
elif r == 0:
Aahz (aahz at           <*>

"At Resolver we've found it useful to short-circuit any doubt and just        
refer to comments in code as 'lies'. :-)"
--Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22

More information about the Python-list mailing list