我在 Python 中的函数的文档字符串中添加了乳胶数学表达式。它会触发错误“W605 无效转义序列”,从而破坏 flake8 检查。如何修复它?
"""
The test function is defined as:
\sin(\pi x)/x
"""
Run Code Online (Sandbox Code Playgroud)
我现在通过使用双斜杠解决了这个问题。
使用原始字符串作为文档字符串
import math
def do_this(x):
r"""This doc string contains a backslash \sin(x)"""
print(math.sin(x))
Run Code Online (Sandbox Code Playgroud)
那么flake8就不会再抱怨了。请参阅提到PEP 257的答案,了解有关当原始文档字符串包含反斜杠时需要原始文档字符串这一事实的更多详细信息。