bidict/docs/caveat-nan-as-key.rst.inc

27 lines
760 B
PHP
Raw Normal View History

2018-01-02 20:32:06 +00:00
.. _nan-as-key:
nan As Key
^^^^^^^^^^
`In CPython <http://doc.pypy.org/en/latest/cpython_differences.html>`_,
nan is especially tricky when used as a dictionary key::
>>> d = {float('nan'): 'nan'}
>>> d
{nan: 'nan'}
>>> d[float('nan')] # doctest: +SKIP
Traceback (most recent call last):
...
KeyError: nan
>>> d[float('nan')] = 'not overwritten'
>>> d # doctest: +SKIP
{nan: 'nan', nan: 'not overwritten'}
In other Python implementations such as PyPy,
nan behaves just like any other dictionary key.
2016-06-28 04:05:22 +00:00
But in CPython, beware of this unexpected behavior,
which applies to bidicts too.
bidict contains no special-case logic
for dealing with nan as a key,
so the behavior will match dict's in the host environment.