Ok, I do see your point, but how would one pass in a custom comparison function to sorted? <br><br>
<div class="gmail_quote">On Jan 7, 2008 11:55 PM, Guido van Rossum <<a href="mailto:guido@python.org">guido@python.org</a>> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">
<div>
<div></div>
<div class="Wj3C7c">On Jan 7, 2008 8:48 PM, hashcollision <<a href="mailto:hashcollision@gmail.com">hashcollision@gmail.com</a>> wrote:<br>><br>><br>> > But the biggest thing missing is precise semantics. Saying "exactly
<br>> > the same semantics as with Python 2.5" doesn't cut it (those semantics<br>> > are incredibly hairy and sometimes surprising, and their<br>> > implementation was a nightmare -- I've rarely been as relieved as when
<br>> > I was able to cut those out of the implementation).<br>><br>><br>> Why is that so? I might be being naive, but what is wrong with something<br>> like this:<br>><br>> def cmp(a, b):<br>> if hasattr(a, '__cmp__'):
<br>> return a.__cmp__(b)<br>> elif hasattr(b, '__cmp__'):<br>> return -1 * b.__cmp__(a)<br>> elif hasattr(a, '__le__') and hasattr(a, '__ge__'):<br>> x = a <= b
<br>> y = a >= b<br>> if x and y:<br>> return 0<br>> elif x:<br>> return -1<br>> elif y:<br>> return 1<br>> elif hasattr(b, '__le__') and hasattr(b, '__ge__'):
<br>> x = b <= a<br>> y = b >= a<br>> if x and y:<br>> return 0<br>> elif x:<br>> return 1<br>> elif y:<br>> return -1
<br>> elif hasattr(a, '__eq__'):<br>> if a == b:<br>> return 0<br>> if hasattr(a, '__lt__'):<br>> if a < b:<br>> return -1<br>
> else:<br>> return 1<br>> elif hasattr(a, '__gt__'):<br>> if a > b:<br>> return 1<br>> else:<br>> return -1
<br>> else:<br>> raise NotImplemented()<br>> elif hasattr(b, '__eq__'):<br>> if a == b:<br>> return 0<br>> if hasattr(b, '__lt__'):<br>
> if b < a:<br>> return 1<br>> else:<br>> return -1<br>> elif hasattr(b, '__gt__'):<br>> if b > a:<br>> return -1
<br>> else:<br>> return 1<br>> else:<br>> raise NotImplemented()<br>> else:<br>> raise NotImplemented()<br><br></div></div>You don't call that hairy? :-)
<br><br>Anyway, one of the additional complications is that it is supposed to<br>be implemented in C, and there is one C slot for all six comparisons<br>together (which of the six is an argument). The 2.x rules are also<br>
further complicated in that sometimes the RHS is tried before the LHS<br>(in case the RHS is an instance of a subclass of the class of the<br>LHS).<br><br>And I think you've ignored the possibility that a.__cmp__(b) might
<br>return NotImplemented.<br><br>Just believe me, it's hairy.<br>
<div>
<div></div>
<div class="Wj3C7c"><br>--<br>--Guido van Rossum (home page: <a href="http://www.python.org/~guido/" target="_blank">http://www.python.org/~guido/</a>)<br></div></div></blockquote></div><br>