[Python-Dev] PyObject_RichCompareBool identity shortcut

Glenn Linderman v+python at g.nevcal.com
Thu Apr 28 03:15:02 CEST 2011


On 4/27/2011 5:05 PM, Steven D'Aprano wrote:
>
> (2) slow containers down by guaranteeing that they will use __eq__;
>
> (but how much will it actually hurt performance for real-world cases? 
> and this will have the side-effect that non-reflexivity will propagate 
> to containers) 

I think it is perfectly reasonable that containers containing items with 
non-reflexive equality should sometimes have non-reflexive equality also 
(depends on the placement of the item in the container, and the values 
of other items, whether the non-reflexive equality of an internal item 
will actually affect the equality of the container in practice).

I quoted the docs for tuple and list comparisons in a different part of 
this thread, and for those types, the docs are very clear that the items 
must compare equal for the lists or tuples to compare equal.  For other 
built-in types, the docs are less clear:

    *

      Mappings (dictionaries) compare equal if and only if they have the
      same (key, value) pairs. Order comparisons ('<', '<=', '>=', '>')
      raise TypeError
      <http://docs.python.org/py3k/library/exceptions.html#TypeError>.

So we can immediately conclude that mappings do not provide an ordering 
for sorts.  But, the language "same (key, value)" pairs implies identity 
comparisons, rather than equality comparisons.  But in practice, 
equality is used sometimes, and identity sometimes:

 >>> nan = float('NaN')
 >>> d1 = dict( a=1, nan=2 )
 >>> d2 = dict( a=1, nan=2.0 )
 >>> d1 == d2
True
 >>> 2 is 2.0
False

"nan" and "nan" is being compared using identity, 2 and 2.0 by 
equality.  While that may be clear to those of you that know the 
implementation (and even have described it somewhat in this thread), it 
is certainly not clear in the docs.  And I think it should read much 
more like lists and tuples... "if all the (key, value) pairs, considered 
as tuples, are equal".

    *

      Sets and frozensets define comparison operators to mean subset and
      superset tests. Those relations do not define total orderings (the
      two sets {1,2} and {2,3} are not equal, nor subsets of one
      another, nor supersets of one another). Accordingly, sets are not
      appropriate arguments for functions which depend on total
      ordering. For example, min()
      <http://docs.python.org/py3k/library/functions.html#min>, max()
      <http://docs.python.org/py3k/library/functions.html#max>, and
      sorted()
      <http://docs.python.org/py3k/library/functions.html#sorted>
      produce undefined results given a list of sets as inputs.

This clearly talks about sets and subsets, but it doesn't define those 
concepts well in this section.  It should refer to where it that concept 
is defined, perhaps.  The intuitive definition of "subset" to me is if, 
for every item in set A, if an equal item is found in set B, then set A 
is a subset of set B.  That's what I learned back in math classes.  
Since NaN is not equal to NaN, however, I would not expect a set 
containing NaN to compare equal to any other set.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20110427/62ed4c2f/attachment-0001.html>


More information about the Python-Dev mailing list