Docstring invalid escape sequence triggered by a display equation in latex
Asked Answered
G

1

7

I put a latex math expression in the docstring for a function in Python. It triggers a error "W605 invalid escape sequence" which breaks flake8 checking. How to fix it?

"""
The test function is defined as:
\sin(\pi x)/x
"""

I solved this by using double slashes now.

Grille answered 20/4, 2020 at 0:52 Comment(3)
Are you going to show us the code?Anticlimax
Can you just use \\sin(\\pi x)/x in your string?Bouton
Use a raw string.Tafoya
N
6

Use a raw string for the docstring

import math
def do_this(x):
    r"""This doc string contains a backslash \sin(x)"""
    print(math.sin(x))

Then flake8 will not complain anymore. See this answer mentioning PEP 257 for more details on the fact that raw doc strings are needed when they contain backslashes.

Norsworthy answered 15/9, 2021 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.