osdir.com


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

Why does __ne__ exist?


On Tue, Jan 9, 2018 at 3:25 AM, Cody Piersall <cody.piersall at gmail.com> wrote:
>> 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.
>
> 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]
>
> Anybody with familiarity with NumPy appreciates that this is possible.

I've used it, and I'm familiar with it, and I'm still not sure that I
appreciate it. But if it's there because of MATLAB, well, I guess
that's what people want?

ChrisA