Why does __ne__ exist?

Antoon Pardon antoon.pardon at rece.vub.ac.be
Mon Jan 8 12:40:04 EST 2018


Op 08-01-18 om 17:25 schreef Cody Piersall:
>> Let's put it this way. Suppose that __eq__ existed and __ne__ didn't,
>> just like with __contains__. Go ahead: sell the notion of __ne__.
>> Pitch it, show why we absolutely need to allow this. Make sure you
>> mention the potential confusion when subclassing. Be sure to show why
>> it's okay for "not in" to force to boolean but "==" should allow any
>> return value.
> 
> __ne__ and __eq__ are important for building mask arrays in NumPy,
> which allow complex indexing operations.  A lot of NumPy's design was
> inspired by MATLAB, so being able to index the same way as in MATLAB
> is a pretty killer feature.

They are only important if you find it necessary to build these mask
arrays wih an operator.

> Indexing an array using mask arrays like this is idiomatic:
> 
> some_arr = np.array([-1, 0, 1, 2, 3, 4, 5, 2, -1, 3, -1, 6, 7, 3])
> valid = some_arr[some_arr != -1]

I guess it was rather useful because list comprehension wasn't included
in the language at that moment, but I don't find it that much more useful
than:

   valid = somearr[[number != -1 for number in somearr]]

-- 
Antoon Pardon.



More information about the Python-list mailing list