[Python-Dev] PyObject_RichCompareBool identity shortcut

Steven D'Aprano steve at pearwood.info
Thu Apr 28 19:26:48 CEST 2011


Rob Cliffe wrote:

> To me the idea of non-reflexive equality (an object not being equal to 
> itself) is abhorrent.  Nothing is more likely to put off new Python 
> users if they happen to run into it.

I believe that's a gross exaggeration. In any case, that's just your 
opinion, and Python is hardly the only language that supports (at least 
partially) NANs.

Besides, floats have all sorts of unintuitive properties that go against 
properties of real numbers, and new users manage to cope.

With floats, even ignoring NANs, you can't assume:

a*(b+c) == a*b + a*c
a+b+c = c+b+a
1.0/x*x = 1
x+y-x = y
x+1 > x

or many other properties of real numbers. In real code, the lack of 
reflexivity for NANs is just not that important. You can program for 
*years* without once accidentally stumbling over one, whereas you can't 
do the simplest floating point calculation without stubbing your toes on 
things like this:

 >>> 1.0/10
0.10000000000000001

Search the archives of the python-list at python.org mailing list. You will 
find regular questions from newbies similar to "Why doesn't Python 
calculate 1/10 correctly, is it broken?"

(Except that most of the time they don't *ask* if it's broken, they just 
declare that it is.)

Compared to that, which is concrete and obvious and frequent, NANs are 
usually rare and mild.

The fact is, NANs are useful. Less useful in Python, which goes out of 
the way to avoid generating them (a pity, in my opinion), but still useful.


> Basically it's deferring to a wart, of dubious value, in floating point 
> calculations and/or the IEEE754 standard, and allowing it to become a 
> monstrous carbuncle disfiguring the whole language.

A ridiculous over-reaction. How long have you been programming in 
Python? Months? Years? If the language was "disfigured" by a "monstrous 
carbuncle", you haven't noticed until now.


> I think implementations of equal/not-equal which are make equality 
> non-reflexive (and thus break "identity implies equality") should be 
> considered broken.

Then Python is broken by design, because by design *all* rich comparison 
methods can do anything.


> On 27/04/2011 15:53, Guido van Rossum wrote:
>> Maybe we should just call off the odd NaN comparison behavior?
> Right on, Guido.  (A pity that a lot of people don't seem to be listening.)

Oh we're listening. Some of us are just *disagreeing*.



-- 
Steven



More information about the Python-Dev mailing list