6

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.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
susanna
  • 1,395
  • 3
  • 20
  • 32

1 Answers1

4

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.

Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110