Why does this work: 5<"five" ?
Kragen Sitaker
kragen at pobox.com
Mon Jun 3 00:03:52 EDT 2002
Jiri Baum <jiri at baum.com.au> writes:
> It's documented that way. When comparing values of different types, you get
> an arbitrary (but fixed) ordering. I think the docs say it can change from
> version to version.
>
> You'll note that 5>"five" gives you a 0.
>
> This means you can do things like binary search trees without worrying
> about how to sort disparate values.
That's what I thought, too, but (in 2.1.1):
>>> 1<1j
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot compare complex numbers using <, <=, >, >=
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 thought maybe tuple comparison
did an 'is' before doing an actual comparison, but timing suggests
that it doesn't.
More information about the Python-list
mailing list