python / python/cpython

Lacking description of how to correctly implement indexing syntax support.

未關閉
#120,802 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

docs
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

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
  • gh-120942
  • gh-122178
  • gh-141124

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

length_hint 描述之後的 Python 資料模型文件區段開始,按照 issue 中連結的參考資料進行。將提議的變更與已連結的 PR gh-120942、gh-122178 和 gh-141124 進行比較;當準確記錄了所列出的 getitem、切片、迭代和 reversed 協定行為,並解決了過時的迴圈註記時,即視為完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
documentation
Issue 類型
文件
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。