I just don't get it

Stephen Horne steve at ninereeds.fsnet.co.uk
Fri Feb 27 19:14:29 EST 2004


On 27 Feb 2004 15:16:32 -0800, eelgueta at navix.cl (Eduardo Elgueta)
wrote:

>Hi All,
>
>Would anyone be so kind to explain this?
>
>-- begin --
>>>> (176L,) > 300
>True
>--end--
>
>(Python2.3)
>
>TIA.
>
>Ed.

Those are different data types - you are comparing a tuple with a
number. When different data types are compared, Python uses an
arbitrary but consistent ordering. This can be very useful at times,
for example when maintaining and searching a sorted list.

If Python started trying to guess your intentions, and assumed that
you really meant to compare the values, a whole load of quite basic
things wouldn't work. Your example with different numbers may seem
obvious, but what if the numbers were the same? Which of the following
should return true?

  (50,) <  50
  (50,) == 50
  (50,) >  50

If you think the middle one, what if the difference between having a
tuple and a number is important?

If you think that all should return false, you're not going to have
much luck with a lot of standard algorithms - it would violate a basic
assumption about ordering relations.

Personally, I might have gone with 'it makes no sense to compare
different types so raise an exception'. Possibly. But probably not.
Pythons system works very well in practice.


In Python, if you want to compare the actual numbers you need to
compare the actual numbers. It works out better in the long run when
your language isn't always second guessing what it thinks you really
meant.


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk



More information about the Python-list mailing list