在编写 python 文档测试时,如何在测试中正确地在字符串中引入换行符?这是一个简单的例子:
def remove_newlines(text):
"""
>>> remove_newlines("line1 \n"
... "still line 1\r"
... "now line2 \n"
... "more line2\n")
line1 still line1
now line2 more line2
"""
return text.replace('\n', '')
import doctest
doctest.run_docstring_examples(remove_newlines, globals())
其输出为:
Traceback (most recent call last):
...
ValueError: line 3 of the docstring for NoName has inconsistent leading whitespace: '"'