Skip to content

Add notes about s[i] in Common Sequence Operations #134976

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
Yzi-Li opened this issue May 31, 2025 · 4 comments
Closed

Add notes about s[i] in Common Sequence Operations #134976

Yzi-Li opened this issue May 31, 2025 · 4 comments
Labels
docs Documentation in the Doc dir

Comments

@Yzi-Li
Copy link
Contributor

Yzi-Li commented May 31, 2025

Documentation

Operation Result Notes
s[i] ith item of s, origin 0 (3)
  1. If i or j is negative, the index is relative to the end of sequence s: len(s) + i or len(s) + j is substituted. But note that -0 is still 0.

We can add a note: If the sequence is empty or i is outside the sequence range, IndexError will be raised.

Linked PRs

@picnixz
Copy link
Member

picnixz commented May 31, 2025

I'm surprised that there is nothing about IndexError on this page

@terryjreedy
Copy link
Member

I believe that while negative indexes are implemented for (most?) builtins, this is not necessarily the case for all sequences.

@picnixz
Copy link
Member

picnixz commented May 31, 2025

Note (3) still says:

If i or j is negative, the index is relative to the end of sequence s: len(s) + i or len(s) + j is substituted. But note that -0 is still 0.

So the common sequence operations assume that negative indices should be supported by arbitrary sequences.

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 8, 2025
…[i]` (pythonGH-134977)

(cherry picked from commit 158e516)

Co-authored-by: Yongzi Li <204532581+Yzi-Li@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 8, 2025
…[i]` (pythonGH-134977)

(cherry picked from commit 158e516)

Co-authored-by: Yongzi Li <204532581+Yzi-Li@users.noreply.github.com>
picnixz pushed a commit that referenced this issue Jun 8, 2025
…s[i]` (GH-134977) (#135258)

gh-134976: document the exception type that can be raised by `s[i]` (GH-134977)
(cherry picked from commit 158e516)

Co-authored-by: Yongzi Li <204532581+Yzi-Li@users.noreply.github.com>
picnixz pushed a commit that referenced this issue Jun 8, 2025
…s[i]` (GH-134977) (#135259)

gh-134976: document the exception type that can be raised by `s[i]` (GH-134977)
(cherry picked from commit 158e516)

Co-authored-by: Yongzi Li <204532581+Yzi-Li@users.noreply.github.com>
@picnixz
Copy link
Member

picnixz commented Jun 8, 2025

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
Status: Todo
Development

No branches or pull requests

3 participants