Skip to content

__index__ method #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
asmeurer opened this issue Jul 16, 2021 · 1 comment · Fixed by #263
Closed

__index__ method #231

asmeurer opened this issue Jul 16, 2021 · 1 comment · Fixed by #263
Labels
API extension Adds new functions or objects to the API. topic: Indexing Array indexing.

Comments

@asmeurer
Copy link
Member

I just noticed that the __index__ method is missing from the array object specification. See https://www.python.org/dev/peps/pep-0357/.

__index__ should return int(self) if the input is a 0-D array with integer dtype, and raise TypeError otherwise (similar to __int__ except it doesn't work on floating-point arrays). This will allow 0-D integer arrays to be used as indices. While this is not strictly necessary for an array type to support itself as an index, it does mean that things like indexing Python lists with 0-D arrays will work correctly, and it's generally a good Python practice to do this for integer-like types.

@rgommers
Copy link
Member

I just did a quick check that NumPy, PyTorch, JAX and TensorFlow all support this behavior. So seems reasonable. Size-1 lists don't support it, so it's not obvious that it should work. But may as well, since it's common practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API extension Adds new functions or objects to the API. topic: Indexing Array indexing.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants