autopep8 extension will insert a backslash to break a long line into multiple lines when formatting Python code

In Visual Studio Code, the Python extension uses autopep8 extension to format the code. However, it will break a long line to multiple lines using backslash "\" for better readability.

 

Solution:

If you have a long line of code that you don’t want to be split into multiple lines with the \ character, you can add # nopep8 at the end of the line to prevent autopep8 from formatting that specific line. Here’s an example:

my_long_line = "This is a very long line of code that I don't want to be split into multiple lines"  # nopep8

 

There is also a permanent setting for this. Go to VS Code Settings, find the autopep8 extension settings. Then click on Add Item and type --max-line-length=120 (change 120 to your preferred length).

创建时间:8/31/2023 5:03:12 PM 修改时间:8/31/2023 5:20:15 PM