[Python-Dev] PyObject_RichCompareBool identity shortcut

Alexander Belopolsky alexander.belopolsky at gmail.com
Thu Apr 28 10:30:56 CEST 2011


On Thu, Apr 28, 2011 at 3:57 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
..
>> It is an interesting question of what "sane invariants" are.  Why you
>> consider the invariants that you listed essential while say
>>
>> if c1 == c2:
>>   assert all(x == y for x,y in zip(c1, c2))
>>
>> optional?
>
> Because this assertion is an assertion about the behaviour of
> comparisons that violates IEEE754, while the assertions I list are all
> assertions about the behaviour of containers that can be made true
> *regardless* of IEEE754 by checking identity explicitly.
>

AFAIK, IEEE754 says nothing about comparison of containers, so my
invariant cannot violate it.  What you probably wanted to say is that
my invariant cannot be achieved in the presence of IEEE754 conforming
floats, but this observation by itself does not make my invariant less
important than yours.  It just makes yours easier to maintain.

> The correct assertion under Python's current container semantics is:
>
>  if list(c1) == list(c2):  # Make ordering assumption explicit
>    assert all(x is y or x == y for x,y in zip(c1, c2))  # Enforce reflexivity
>

Being correct is different from being important.  What practical
applications of lists containing NaNs do this and your other
invariants enable?  I think even with these invariants in place one
should either filter out NaNs from their lists or replace them with
None before doing applying container operations.


More information about the Python-Dev mailing list