Comparison of different types does not throw exception

Gordon Williams g_will at cyberus.ca
Fri Jul 13 08:29:12 EDT 2001


I agree that allowing == and != is a requirement for objects of different
types but allowing < = and > type operations is questionable.

A couple of people have brought up ordering of heterogeneous lists.  First,
I don't know why anyone would want to do this.  (Can anyone give me an
example where they have done this for a "real world" problem?).  According
to Harms and McDonnald, the order of objects is arbitrary and only
guaranteed to be fixed for one version of python; the order may be different
in the next version.  Second, if this is *really* a requirement,
heterogeneous lists could still be ordered while not allowing <> operators
to act on different object types.  I think that leaving heterogeneous list
sorting out would not break too much code.

It is never too late to correct a design flaw :)

Regards,

Gordon Williams


Guido van Rossum wites:

"Gordon Williams" <g_will at c...> 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