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:.
- Translation of
a[1:2, 3:5] into a[(slice(1, 2, None), slice(3, 5, None))].
- Because that translation exist, custom behaviour types (like matrix) are also supported.
- Slicing should return object of the same type, or at least similar.
- 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__).
- One object can implement both protocols correctly (at the same time).
- Setting
__iter__ to None makes iter not fallback to sequence protocol. (And similar point for reversed).
- Having ABC as parent, or explicitly assigning methods from them, isn't worst than not defining them at all.
- (After coping fallback description from
__reversed__, correct that) iter don't need full sequence protocol, just __getitem__ method.
Linked PRs
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:.a[1:2, 3:5]intoa[(slice(1, 2, None), slice(3, 5, None))].iterandnext, the iterator calls__getitem__).__iter__toNonemakesiternot fallback to sequence protocol. (And similar point forreversed).__reversed__, correct that)iterdon't need full sequence protocol, just__getitem__method.Linked PRs
__getitem__. #120942__getitem__. #122178