osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Why does __ne__ exist?


07.01.18 22:33, Chris Angelico ????:
> On Mon, Jan 8, 2018 at 7:13 AM, Thomas Jollans <tjol at tjol.eu> wrote:
>> On 07/01/18 20:55, Chris Angelico wrote:
>>> Under what circumstances would you want "x != y" to be different from
>>> "not (x == y)" ?
>>
>> In numpy, __eq__ and __ne__ do not, in general, return bools.
>>
>>>>> a = np.array([1,2,3,4])
>>>>> b = np.array([0,2,0,4])
>>>>> a == b
>> array([False,  True, False,  True], dtype=bool)
>>>>> a != b
>> array([ True, False,  True, False], dtype=bool)
> 
> Thanks, that's the kind of example I was looking for. Though numpy
> doesn't drive the core language development much, so the obvious next
> question is: was this why __ne__ was implemented, or was there some
> other reason? This example shows how it can be useful, but not why it
> exists.

AFAIK this was the main reason. This can be also used for creating queries.

NumPy inspired 4 or 5 core features which are rarely used outside of 
NumPy. They include the possibility of comparison operators to return 
non-booleans.