[Python-Dev] decimal.py: == and != comparisons involving NaNs

Antoine Pitrou solipsis at pitrou.net
Mon Nov 9 12:21:27 CET 2009


Stefan Krah <stefan-usenet <at> bytereef.org> writes:
> 
> >>> d = {0:Decimal("NaN")}
> >>> Decimal("NaN") in d.values()
> False
> 
> So, since non-decimal use cases are limited at best, the equality/inequality
> operators might as well have the behavior of the other comparison operators,
> which is safer for the user.

The problem is when searching for /another/ object which hashes the same as
Decimal("NaN"). Here is a made-up situation to show you the problem:

>>> class H(object):
...   def __hash__(self): return hash(1)
...   def __eq__(self, other): raise ValueError
... 
>>> h = H()
>>> d = {h: ""}
>>> d[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __eq__
ValueError
>>> d[1] = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __eq__
ValueError





More information about the Python-Dev mailing list