python / python/cpython

Defaultdict docs could be clearer

未關閉
#156,950 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

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

描述

Documentation

The first three paragraphs of the current defaultdict class docs currently read:

Return a new dictionary-like object. defaultdict is a subclass of the built-in dict class. It overrides one method and adds one writable instance variable. The remaining functionality is the same as for the dict class and is not documented here.
The first argument provides the initial value for the default_factory attribute; it defaults to None. All remaining arguments are treated the same as if they were passed to the dict constructor, including keyword arguments.
defaultdicts are generic over two types, signifying (respectively) the types of the dictionary’s keys and values.

Nowhere in this do we learn about what the defaultdict does differently from dict. We don't even learn what the default_factory parameter is. In fact, the description of the default_factory attribute is decidedly uninformative as well:

This attribute is used by the missing() method; it is initialized from the first argument to the constructor, if present, or to None, if absent.

Basically, I need to read the documentation of the dunder method __missing__ to learn what imo should be stated somewhere at the top earlier.

Suggestions:

  1. Have a few beginner-friendly introductory sentences that explain in plain language what defaultdict is / how its behaviour differs from dict.

For example, the first sentence of the namedtuple section is
"Named tuples assign meaning to each position in a tuple and allow for more readable, self-documenting code." The utility / motivation for defaultdict is explained well in the Examples section so that doesn't need to be stated (and it feels hard to state its utility without examples).

  1. Expand the default_factory attribute documentation so it is more standalone. At the very least it should state that it should be a callable? The fact that the default_factory parameter must be a callable (or None) should also be stated up top, since the code otherwise throws a TypeError.

Some previous discussions: #131490, #85787 #53745

Happy to try a PR. :-)

Linked PRs
  • gh-157033

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

從 issue 中連結的目前 defaultdict 類別文件開始,將其介紹段落和 default_factory 屬性說明與所要求的改進進行比較。完成的標準是:文件解釋 defaultdict 與 dict 的差異,將 default_factory 描述為可呼叫物件或 None,並提供一份獨立的說明,無需讀者從 missing() 開始閱讀。

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

評估

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

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

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