Bingo. That clinches it. We need to add key=.<br><br><div class="gmail_quote">On Thu, Feb 9, 2012 at 6:27 AM, Arnaud Delobelle <span dir="ltr"><<a href="mailto:arnodel@gmail.com">arnodel@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On 9 February 2012 00:25, Guido van Rossum <<a href="mailto:guido@python.org">guido@python.org</a>> wrote:<br>


> Basically Raymond says "bisect can call the key() function many times, which<br>
> leads to bad design". His alternative, to use a list of (key, value) tuples,<br>
> is often a bit clumsy when passing the sorted list to another function (e.g.<br>
> for printing); having to transform the list using e.g. [v for (k, v) in a]<br>
> feels clumsy and suboptimal.<br>
<br>
</div>Also, in Python 3 one can't assume that values will be comparable so<br>
the (key, value) tuple trick won't work: comparing the tuples may well<br>
throw a TypeError.  Here's a simple example below.  The class 'Person'<br>
has no natural order, but we may want to keep a list of people sorted<br>
by iq:<br>
<br>
>>> class Person:<br>
...     def __init__(self, height, iq):<br>
...         self.height = height<br>
...         <a href="http://self.iq" target="_blank">self.iq</a> = iq<br>
...<br>
>>> arno = Person(184, 101)<br>
>>> guido = Person(179, 185)<br>
>>> steve = Person(168, 101)<br>
>>> key = lambda p: <a href="http://p.iq" target="_blank">p.iq</a><br>
>>> people = []<br>
>>> bisect.insort(people, (key(arno), arno))<br>
>>> bisect.insort(people, (key(guido), guido))<br>
>>> bisect.insort(people, (key(steve), steve))<br>
Traceback (most recent call last):<br>
  File "<stdin>", line 1, in <module><br>
TypeError: unorderable types: Person() < Person()<br>
>>><br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Arnaud<br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br>--Guido van Rossum (<a href="http://python.org/~guido">python.org/~guido</a>)<br>