python / python/cpython

Defaultdict docs could be clearer

Ouverte
#156,950 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

docs
Langage dominant
Python
Étoiles
77.2k
Forks
35.9k
Métriques de merge des PR
Métriques de PR en attente

Description

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par la documentation actuelle de la classe defaultdict liée dans l’issue et comparez ses paragraphes introductifs ainsi que la description de l’attribut default_factory avec les améliorations demandées. Le travail est considéré comme terminé lorsque la documentation explique la différence entre defaultdict et dict, décrit default_factory comme un callable ou None et fournit une explication autonome sans exiger que les lecteurs commencent par missing().

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
documentation
Type d'issue
Documentation
Difficulté
2/5
Temps estimé
1-3 heures
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.