Comparison of different types does not throw exception

David Bolen db3l at fitlinxx.com
Thu Jul 12 19:23:15 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:

Um, because it might not be exceptional?

> >>> 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.

But it may not be a programming error, depending on what you are
trying to achieve.  Heterogeneous collections of objects may still have
a relative sort order amongst entries in the collection, even if in
your specific case here you don't expect that.  There may not be all
that many cases when this is used (although sorting such a
heterogeneous list, for example, is one)

In Python, any object that implements appropriate methods can be used
in a comparison, and all the native objects do.  It is possible in
later releases (when the rich comparisons change was implemented) that
objects can throw exceptions in such a comparison, but just comparing
objects of two types is not normally likely to do so.

> This one tripped me up in an assert statement for a couple of hours.  The
> assert statement was returning true when it should have been false to give
> me a warning that there was a problem.  It was something like:

Ah, but saying it "should have been false" is only because you
believed it was testing something you didn't ask it to test.

> lengthA= 3
> lengthB= "2"
> 
> assert lengthA <= lengthB
> 
> and was returning true
> 
> 
> It would have been *much* more helpful if it told be that I was comparing
> objects of different types.

But if that's what you wanted to assert, then you should adjust your
assert to check that fact rather than assuming that an ordering
comparision would indirectly raise an exception.

Because of Python's polymorphism, any function (method, etc..) that is
going to accept objects needs to expect that arbitrary object types
may be supplied.  The logical comparison functions also work
polymorphically.  Now if a function is not truly polymorphic, but only
works on certain types of parameters or combinations of parameters,
then it should probably check that fact explicitly - either by
ensuring the objects implement the right methods, or if they need to
be the same type, that they are in fact so.

So, you might add to your function:

    assert type(lengthA) == type(lengthB)

Section 2.1.3 (Comparisons) in the library reference covers this
aspect of comparisons explicitly.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list