How does compare work?

Helmut Jarausch jarausch at skynet.be
Tue Jan 27 12:44:09 EST 2004


Terry Reedy wrote:
> "Helmut Jarausch" <jarausch at skynet.be> wrote in message
> news:40158680$0$7044$ba620e4c at news.skynet.be...
> 
>>what does Python do if two objects aren't comparable (to my opinion)
> 
> 
> Sorry, Python does what it does, which is generally to compare any two
> objects unless explicitly disabled as with complex numbers or in
> user-written method of user class.
> 
> 
>>If I've understood "Python in a Nutschell" correctly it should raise an
>>exception but it doesn't do for me.
> 
> 
> Perhaps you could quote the line that mislead you, and someone could
> explain, or suggest a change to the author.
> 

First let me say that IMHO this is a big misdesign of Python.
I've come to Python from Perl since many posts convinced me that Python
is safer - and mostly it is.
But silently(!) comparing apples with pears is evil. E.g. the example I 
came across this was

thresh=raw_input('enter threshold')
...
level=2
...
if level > thresh :

which failed miserably. By the way, Perl does convert the string to an 
integer silenty and, as most of the time, these Perl conversions are 
just what one wants, so here.
Nevertheless, I'd prefer an exception in this case since automatic 
conversions can never be right in all circumstances.
So this is a case where Python is a very dangerous language!

For the quotation
In my 'Python in a Nutshell' by Alex Martelli (a tremendously good 
book), 1st edition on page 91 (Special Methods) it reads
__cmp__  ......
...............
          When __cmp__ is also absent, order comparisons (<,<=,>,>=)
          raise exceptions. Equality comparisons (==,!=), in this case,
          become identity checks: x==y evaluates  id(x)==id(y) (i.e.,
          x is y)

end of quotation.
I wish this were true for the current Python implementation.

Helmut.

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany




More information about the Python-list mailing list