Skip to content

Lacking description of how to correctly implement indexing syntax support. #120802

@Alex-Wasowicz

Description

@Alex-Wasowicz

Documentation

During a discussion on idea of minimizing requirements for children of collections.abc.Sequence, it was pointed out, that documentation is currently lacking sufficient description on expected behaviour of method __getitem__.

The part in question starts with first note after __length_hint__ description. I started by writing, to see what's missing. Non-trivial changes include:.

  1. Translation of a[1:2, 3:5] into a[(slice(1, 2, None), slice(3, 5, None))].
  2. Because that translation exist, custom behaviour types (like matrix) are also supported.
  3. Slicing should return object of the same type, or at least similar.
  4. If description is good, then note about for loops is unnecessary. (Plus it's outdated, for loops call iter and next, the iterator calls __getitem__).
  5. One object can implement both protocols correctly (at the same time).
  6. Setting __iter__ to None makes iter not fallback to sequence protocol. (And similar point for reversed).
  7. Having ABC as parent, or explicitly assigning methods from them, isn't worst than not defining them at all.
  8. (After coping fallback description from __reversed__, correct that) iter don't need full sequence protocol, just __getitem__ method.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions