cpython icon indicating copy to clipboard operation
cpython copied to clipboard

Add examples to docs for str methods

Open hauntsaninja opened this issue 2 years ago • 10 comments

@adorilson already opened a PR for this over at https://github.com/python/cpython/pull/105670. If people have higher level feedback on the suggestion, this issue is a good place to do it.

Maybe cc @CAM-Gerlach in case you're interested :-)

Linked PRs

  • gh-105670
  • gh-111743
  • gh-119445
  • gh-134272
  • gh-134518
  • gh-134519
  • gh-134520
  • gh-134523
  • gh-134525
  • gh-134571
  • gh-134572
  • gh-134573
  • gh-134574
  • gh-134529
  • gh-135457
  • gh-135458
  • gh-135459
  • gh-135460
  • gh-135476
  • gh-135477
  • gh-137018
  • gh-137023
  • gh-137550
  • gh-137557
  • gh-137558
  • gh-137559
  • gh-138174
  • gh-138175
  • gh-140043
  • gh-140046
  • gh-140113
  • gh-140114
  • gh-140314
  • gh-140315
  • gh-141893
  • gh-141894
  • gh-141898
  • gh-141899
  • gh-141901
  • gh-141902
  • gh-141903
  • gh-141904
  • gh-141905
  • gh-141906
  • gh-142154
  • gh-142680
  • gh-142716
  • gh-142715
  • gh-142719
  • gh-142802
  • gh-142803
  • gh-142822
  • gh-142823

hauntsaninja avatar Jul 01 '23 20:07 hauntsaninja

The main question is whether the examples are worth the space. For some readers, details are definitely useful. I wonder if some of them might better be put inline in the description.

terryjreedy avatar Jul 01 '23 21:07 terryjreedy

Hi, folks. Thank you for your review and suggestions. All of them were applied (letting For example at the end of the paragraph). Maybe except in the splitlines method. It is because there is a table between the first paragraph and the example. So, we can:

  1. Leave as is
  2. Move the example (maybe just the first one) to after the first paragraph

Another idea here?

Another thing is about versionadded:: 3.2. What do you think about removing it. The reason is that 3.2 is an old version (after end-of-file)? There are some rules about this?

adorilson avatar Jul 03 '23 11:07 adorilson

The main question is whether the examples are worth the space.

I think the answer is mostly "No". In my courses, I ask people to read the string docs to become familiar with the methods. What I've learned is a) the docs are clear enough without having an example, and b) it already is too long and tedious to read.

IMO, a well-intended effort to add examples would be counter-productive and make the docs less useful. Also, it is already trivially easy to run experiments to verify understanding (the string methods require almost no setup to try out).

FWIW, the argparse docs are a example (no pun intended) of the consequences of going overboard with examples. Instead of one or two informative examples for the whole module, they are were added at a fine-grained level. As a result, we almost never can get anyone to read the docs top to bottom to gain a full understanding of the module.

rhettinger avatar Jul 09 '23 16:07 rhettinger

Perhaps we could put these examples in expandable sections that are hidden by default, like we recently did for the "relevant PEPs" section of the typing docs: https://docs.python.org/3.12/library/typing.html#relevant-peps.

AlexWaygood avatar Jul 09 '23 17:07 AlexWaygood

Perhaps we could put these examples in expandable sections that are hidden by default, like we recently did for the "relevant PEPs" section of the typing docs: https://docs.python.org/3.12/library/typing.html#relevant-peps.

Do you mean something like this? And @rhettinger, what do you think about it?

image

adorilson avatar Sep 17 '23 13:09 adorilson

Do you mean something like this?

yes

AlexWaygood avatar Sep 17 '23 14:09 AlexWaygood

The only problem is that man pages, LaTeX, epub, etc (to my knowledge) don't have the concept of expandable sections. See https://github.com/sphinx-doc/sphinx/pull/10532#issuecomment-1166589848 for some context.

On the other hand, this would be useful even if we make the collapsible content HTML only.

A

AA-Turner avatar Sep 19 '23 16:09 AA-Turner

Hi, people.

I opened a new PR (#111743 ). As said there, I would like a "go ahead" to confirm that the work will be valid.

adorilson avatar Nov 04 '23 22:11 adorilson

We recently had a few discussions related to this, including in the last docs-community meeting or on Discord. The relevant bits are:

  • Adding more examples is generally a good idea. Depending on the context, it can be done inline, at the bottom of the page in an "Examples" section, or as a separate page.
  • There's been some discussion about using collapsible sections (see the issue linked above by @AA-Turner). Using .. raw:: is probably not the right way to go, since there are already extensions that provide the feature. However these are not compatible with all builders, so we can't just use them easily without a fallback for the other builders.

Until a better solution for collapsible sections is found, and if adding more example inline it's not desirable for the str methods, it might be better to just add the examples at the bottom of the page (like I did for the str.format docs) or possibly in a separate page (this needs a broader discussion though, especially if a similar approach gets adopted for builtin functions and possibly other pages).

ezio-melotti avatar Jan 23 '24 18:01 ezio-melotti

Re-opening, mis-auto-closed by GitHub because of the magic "fix" keyword in https://github.com/python/cpython/pull/134518 and others.

hugovk avatar May 23 '25 09:05 hugovk