Comparison of different types does not throw exception

Guido van Rossum guido at python.org
Thu Jul 12 22:08:53 EDT 2001


"Gordon Williams" <g_will at cyberus.ca> writes:

> I would like to know why python does not produce an exception when a
> comparison of objects of different types is made.  For example:
> 
> >>> 1 < "aaa"
> 1
> 
> I cant see why anyone would like to have comparisons of different types as
> it has no meaning.  It is a programming error and an exception should be
> produced.

You have already received several rationalizations for why this should
not be considered an error, but I want to say that this is actually
what I consider a design mistake in Python.  Long ago I thought that
it made sense to want to sort an array of heterogeneous values.  Now
I'm not so sure if that makes sense any more.

If I could change this, I would probably allow "==" and "!="
comparisons between objects of different types, but I would disallow
"<", "<=" etc. (actually, I would leave it up to the types, but most
standard types would do it this way).

But there is probably too much code that would break if we changed the
rules now.  (Although I haven't heard a peep from anyone who was
bitten by the change in 2.1 where we disallow "<" etc. for complex
numbers.  But then if we removed complex numbers, most folks wouldn't
notice anyway, and those folks who use them are wiser than trying to
compare them...)

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-list mailing list