【发布时间】:2019-02-12 06:44:36
【问题描述】:
我正在使用 pytest 检查多行文档字符串,而我测试这些多行 cmets 的方法包括制作一个临时文件并使用 write() 将文档字符串写入然后搜索它。
def test_file_contains_multiline_python_comment_count(tmpdir):
"""Checks that the multiline python comment count works"""
hello_file = tmpdir.mkdir("subdirectory").join("Hello.py")
hello_file.write(""" hello \n world """)
assert hello_file.read() == """ hello \n world """
assert len(tmpdir.listdir()) == 1
comment_count = entities.count_entities(
hello_file.basename, hello_file.dirname, comments.count_multiline_python_comment
)
assert comment_count == 1
但是我无法弄清楚如何编写实际的文档字符串。例如,"""hello""" 将简单地显示为 hello
【问题讨论】:
-
"""只是一种表示多行字符串的 Python 方式。它与文档字符串无关。文档字符串是函数的第一个未分配的字符串。您可以使用任何引号来表示它。打印后,"""X"""、"X"和'X'都将变为X。 -
当然,如果你想用三引号写
string,你可以hello_file.write('"""{}"""'.format(string))。 -
@tripleee 谢谢你,那行得通。也读进去怎么样?只看到一对引文。我如何让它也看到另一个?
-
你应该理解上面的第一条评论。