-
Notifications
You must be signed in to change notification settings - Fork 44
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
docs: add note that iteration is defined for 1-D arrays #821
base: main
Are you sure you want to change the base?
Conversation
@@ -627,6 +629,10 @@ def __getitem__( | |||
------- | |||
out: array | |||
an array containing the accessed value(s). The returned array must have the same data type as ``self``. | |||
|
|||
.. note:: | |||
When ``__getitem__`` is defined on an object, Python will automatically define iteration (i.e., the behavior from ``iter(x)``) as ``x[0]``, ``x[1]``, ..., ``x[N-1]``. This can also be implemented directly by defining ``__iter__``. Therefore, for a one-dimensional array ``x``, iteration should produce a sequence of zero-dimensional arrays ``x[0]``, ``x[1]``, ..., ``x[N-1]``, where ``N`` is the number of elements in the array. Iteration behavior for arrays having more than one dimension is unspecified and thus implementation-defined. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@asmeurer What should happen for zero-dimensional arrays?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a 0-D array x[i]
is an IndexError, so this doesn't really apply (the Python auto-__iter__
would never work). Ideally calling list()
on a 0-D should always be an error, but we can also just change this to say "arrays not having one dimension".
Fixes #818
I also added a cross-reference to the indexing page to the
__getitem__
and__setitem__
docs.