osdir.com


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

Why does __ne__ exist?


On 01/07/2018 12:33 PM, Chris Angelico wrote:
> 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.

Actually, I think it is why it exists.  If I recall correctly, the addition of the six comparative operators* was added 
at the behest of the scientific/numerical community.

--
~Ethan~

* Yeah, I can't remember the cool name for those six operators at the moment.  :(