all Python 3doctestfunction
doctest.script_from_examples()
debugging
Turns a string with >>> examples into a runnable script: code without prompts and the expected output in ## comments.
Syntax
doctest.script_from_examples(s)
It works on a string rather than an object, unlike testsource(). The expected output turns into a comment and is no longer checked: the resulting script only runs the code. Ordinary text between examples becomes # comments.
Examples
import doctest
text = "Пример:\n>>> x = 2\n>>> x * 21\n42\n"
print(doctest.script_from_examples(text))
# → # Пример: / x = 2 / x * 21 / # Expected: / ## 42
See also
Open in the full glossarydocs.python.orgReport an inaccuracy