我在 Python 中的函数的文档字符串中放置了一个 latex 数学表达式。它会触发一个错误“W605 无效转义序列”,这会破坏 flake8 检查。如何解决?
"""
The test function is defined as:
\sin(\pi x)/x
"""
我现在通过使用双斜线解决了这个问题。
最佳答案
使用原始字符串作为文档字符串
import math
def do_this(x):
r"""This doc string contains a backslash \sin(x)"""
print(math.sin(x))
然后 flake8 不会再提示了。见 this answer提PEP 257有关包含反斜杠时需要原始文档字符串这一事实的更多详细信息。
关于python - 由 latex 中的显示方程触发的文档字符串无效转义序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61313624/