python / python/cpython

Defaultdict docs could be clearer

オープン
#156,950 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

docs
主要言語
Python
スター
77.2k
フォーク
35.9k
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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

issue にリンクされている defaultdict クラスの現在のドキュメントから始め、導入段落と default_factory 属性の説明を、要求されている改善内容と比較します。ドキュメントで defaultdict と dict の違いを説明し、default_factory を callable または None として記述し、読者が missing() から読み始める必要のない独立した説明を提供できれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
documentation
issue の種類
ドキュメント
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。