1

I've been helping a new Python user with some code, and he asked me why the python interpreter shows some syntax errors on the line after the actual error. Consider the following code:

x = [1, 2, 3
print x

A syntax error will be shown for the line with print x, although the error is really on the previous line.

This can be very confusing (and time wasting) the first time you run into it, it is a very common issue (search for "python syntax error" on your favourite search engine), and it seems like it wouldn't be difficult to rectify... why hasn't it been fixed? Is there some benefit to the present approach?

7
  • 2
    Because the syntax error isn't on the first line. If you put a closing bracket ] on line 2, this is valid syntax. So there's nothing wrong with line 1. Commented Sep 12, 2017 at 11:23
  • It's only a syntax error when the parser encounters something it wasn't expecting to see given the grammar bit its parsing. So it's easier to think of it as "this is where it started to not make sense" rather than "this is where you need to correct it"... because at that point - it has no idea what you're trying to say - so doesn't have much of an idea of which part is wrong... Commented Sep 12, 2017 at 11:26
  • Wouldn't it be reasonable to show the error as being on the last line with list syntax? I'm sure practically every new python programmer runs into this and ends up wasting time and leaving confused. Commented Sep 12, 2017 at 11:28
  • Alternatively, as mentioned in my other comment, perhaps a more descriptive error would be better, something like, "Broken list on or before this line!" Commented Sep 12, 2017 at 11:31
  • @PProteus you're taking it from the approach that you know what error it is that needs correcting and given that context you can isolate which line, character and remedy to that is... you could have meant x = [1, 2, 3, print] and were doing x on a line by itself... Commented Sep 12, 2017 at 11:32

2 Answers 2

7

It is valid Python syntax to split a list across multiple lines, like so:

x = [1, 2, 3
, 4, 5, 6]
print x

So, the interpreter starts reading the print... line expecting a valid continuation of the list, which it obviously cannot find.

Also, I don't find this terribly misleading. I think it's pretty easy to figure out why you get the error.

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

4 Comments

So wouldn't it be reasonable to put the error on the last line with list syntax?
That depends. In your case, maybe. In case I want my list to span two lines and I forget a comma on the second line, the current error is better.
Perhaps a more descriptive error would be better? Something like "Broken list at or before this line!"
@PProteus Yes, that would probably be more helpful in cases where the error is harder to spot (longer lines, more complex code).
2
x = [1, 2, 3
]
print x

Valid syntax! Because this is possible, the error is actually on line 2 when the list doesn't continue or end.

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.