For ndindex (https://quansight.github.io/ndindex/), the biggest issue
with the API is that to use an ndindex object to actually index an
array, you have to use a[idx.raw] instead of a[idx]. This is because
for NumPy arrays, you cannot allow custom objects to be indices. The
exception is objects that define __index__, but this only works for
integer indices. If __index__ returns anything other than an integer,
you get an IndexError. This is annoying because it's easy to forget to
do this when working with the ndindex API, and the error message from
NumPy isn't informative about what went wrong unless you know to
expect it.
I'd like to propose an API that would allow custom objects to define
how they should be converted to a standard NumPy index, similar to
__index__ but that supports all index types. I think there are two
options here:
- Allow __index__ to return any index type, not just integers. This is
the simplest because it reuses an existing API, and __index__ is the
best possible name for this API. However, I'm not sure, but this may
actually conflict with the text of PEP 357
(https://www.python.org/dev/peps/pep-0357/). Also, some other APIs use
__index__ to check if something is an indexable integer, which
wouldn't accept generic index. For example, elements of a slice can be
any object that defines __index__.
- Add a new __numpy_index__ API that works like
def __numpy_index__(self):
return