Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

asmeurer
Copy link
Member

Fixes #818

I also added a cross-reference to the indexing page to the __getitem__ and __setitem__ docs.

@kgryte kgryte changed the title Note that iteration is defined for 1-D arrays docs: add note that iteration is defined for 1-D arrays Sep 19, 2024
@kgryte kgryte added this to the v2024 milestone Sep 19, 2024
@kgryte kgryte added the Narrative Content Narrative documentation content. label Sep 19, 2024
@@ -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.
Copy link
Contributor

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?

Copy link
Member Author

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".

@kgryte kgryte added the Needs Discussion Needs further discussion. label Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Narrative Content Narrative documentation content. Needs Discussion Needs further discussion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Iteration on 1-D arrays
2 participants