Strange error about invalid syntax

Getting “invalid syntax” on a plain return statement is pretty much impossible. If you use it outside of a function, you get 'return' outside function, if you have the wrong indentation you get IndentationError, etc.

The only way I can get a SyntaxError: invalid syntax on a return statement, is if in fact it doesn’t say return at all, but if it contains non-ascii characters, such as retürn. That give this error. Now, how can you have that error without seeing it? Again, the only idea I can come up with is that you in fact have indentation, but that this indentation is not spaces or tabs. You can for example have somehow inserted a non-breaking space in your code.

Yes, this can happen. Yes, I have had that happen to me. Yes, you get SyntaxError: invalid syntax.

Leave a Comment