Making an object x such that “x in [x]” returns False

前端 未结 2 1092
攒了一身酷
攒了一身酷 2020-12-09 09:09

If we make a pathological potato like this:

>>> class Potato:
...     def __eq__(self, other):
...         return False
...     def __hash__(self):
         


        
2条回答
  •  时光取名叫无心
    2020-12-09 09:36

    In general, breaking the assumption that identity implies equality can break a variety of things in Python. It is true that NaN breaks this assumption, and thus NaN breaks some things in Python. Discussion can be found in this Python bug. In a pre-release version of Python 3.0, reliance on this assumption was removed, but the resolution of the bug was to put it back in (i.e., make Python 3 give the same behavior as Python 2, in which the identity check shortcut is done). The documentation for Python 3 correctly says:

    For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression x in y is equivalent to any(x is e or x == e for e in y).

    However, it appears the documentation for Python 2 is incorrect, since it says:

    For the list and tuple types, x in y is true if and only if there exists an index i such that x == y[i] is true.

    You could raise a documentation bug about this if you want, although it is a pretty esoteric issue so I doubt it will be high on anyone's priority list.

提交回复
热议问题