0

I noticed that if I break up a long line of Python in Sublime Text 3 with comments it marks the top two as being red (and therefore wrong) despite the script working fine.

# This is a long line broken up with comments 
"So I'm ready to attack, gonna lead the pack," + \ # red comment here
"Gonna get a touchdown, gonna take you out," + \ # and here
"That's right, put your pom-poms down, getting everybody fired up" # ok here

Would I have to modify the Sublime config files for Python, in my current syntax style, using a regex to fix this?

1 Answer 1

1

The Python 2.7 Language reference has this to say on explicit line joining:

A line ending in a backslash cannot carry a comment. A backslash does not continue a comment. A backslash does not continue a token except for string literals (i.e., tokens other than string literals cannot be split across physical lines using a backslash). A backslash is illegal elsewhere on a line outside a string literal.

Indeed for both Python 2 and Python 3, the code you posted is not considered valid and generates errors:

  File "/home/tmartin/test.py", line 2
    x = "So I'm ready to attack, gonna lead the pack," +  # red comment here
                                                                           ^
SyntaxError: invalid syntax

Doing this in the Python Online Shell provides a more specific error message:

Python 3.6.0 (default, Jan 13 2017, 00:00:00) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> "So I'm ready to attack, gonna lead the pack," + \ # red comment here
  File "<stdin>", line 1
    "So I'm ready to attack, gonna lead the pack," + \ # red comment here
                                                                        ^
SyntaxError: unexpected character after line continuation character

As such, Sublime seems to be correct in telling you that the code is invalid in this case and I can't see any way to have your code snippet work unaltered without a modified version of the Python interpreter, although I am by no means a Python expert.

That said, assuming that this does indeed work for you, the only way to stop Sublime from showing you those lines as being errors would be for you to manually edit the Python syntax that ships with Sublime to get it to not treat that situation as invalid.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.