Why Python need rich comparison?

python

Solution

NumPy uses rich comparisons to vectorize `==`, `!=`, `<`, etc, just like it does with most other operators. For example,

>>> x = numpy.array([1, 2, 3, 4, 5])
>>> y = numpy.array([2, 2, 1, 4, 4])
>>> x == y
array([False,  True, False,  True, False], dtype=bool)

When arrays `x` and `y` are compared with any comparison operator, NumPy applies the operator (roughly) elementwise and returns an array of results. This is useful, for example, to apply an operation to the cells of `x` that fit the condition:

>>> x[x==y] = 6
>>> x
array([1, 6, 3, 6, 5])

Here, I've selected all elements of `x` that equal the corresponding elements of `y`, and set them equal to 6.

Problem

There is a confusion for me for some time: is there a scene that we do need to use rich comparison in Python? I read the official doc here, but it only gives how it works not why we need it. A snippet of the doc: The truth of `x==y` does not imply that `x!=y` is false. may describe a scene that we need rich comparison. In this scene, we can make `__eq__` and `__ne__` both return `False` for disabling the comparsion or any other purpose. (We can implement this by using `__cmp__`) But this just a guess, I have never encountered such a requirement in a real project yet. Does anyone need to use rich comparison indeed or is there any other scenario where we need to use rich comparison in theory? Maybe my example of `x==y` and `x!=y` caused some confusion, sorry for that. Let me make it a bit clearer: Are there any scenario where rich comparison can help but `__cmp__` can not?

Original source

Related problems