Why does this work: 5<"five" ?

Greg Ewing look at replyto.address.invalid
Tue Jun 4 07:21:57 CEST 2002


Kragen Sitaker wrote:
> 
> So if you put complex numbers into your binary search tree, you will
> have problems.
> 
> Oh, enjoy this:
> >>> (1,) < (2,)
> 1
> >>> (1j,) < (1j,)
> 0
> 
> I'm not sure what's going on there.

I think what's going on is that the tuple elements
are being compared for equality first, and if that
fails, compared again for ordering:

>>> (1j,) < (2j,)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: cannot compare complex numbers using <, <=, >, >=

Incidentally, I believe that Python ought to have
a separate notion of compare-for-arbitrary-ordering
that can be explicitly invoked for things like the
b-tree situation. In fact, I think that's what
cmp() should do. But currently it doesn't -- it
seems that if the inequality operators don't work
on a type, cmp() won't work either.

-- 
Greg Ewing, Computer Science Dept, 
University of Canterbury,	  
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list