learning python, coming from a php background. Keeping it short. Is there a way to get more descriptive error messages about the syntax error? like in php. Example: print var should give something like "expecting ( " , instead of the standard "SyntaxError: invalid syntax"
Thanks
You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.
Syntax errors are produced by Python when it is translating the source code into byte code. They usually indicate that there is something wrong with the syntax of the program. Example: Omitting the colon at the end of a def statement yields the somewhat redundant message SyntaxError: invalid syntax.
Raised when the parser encounters a syntax error. This may occur in an import statement, in an exec statement, in a call to the built-in function eval() or input(), or when reading the initial script or standard input (also interactively).
Syntax errors are mistakes in the use of the Python language, and are analogous to spelling or grammar mistakes in a language like English: for example, the sentence Would you some tea? does not make sense – it is missing a verb. Common Python syntax errors include: leaving out a keyword.
My advice would be to use a solid IDE. I only tried a few before settling down for Aptana studio which is free (as in freedom) and cross-platform, but I am sure that many other offer similar functionality.
IDE with syntax highlighting will try to parse your code even before runtime, and will signal you any mistake (sometimes they have "false positives" but this is the exception, they are normally very accurate).
Here's a screenshot that illustrate how it works.
This is the wrong code:
And this is the popup that shows when you hover over the red X mark.
As delnan correctly pointed out in the comments to your question, there are a lot of possible "right" things that can go after a print
, hence the verbosity of the popup. Nevertheless I find this feature very useful, as it also shows you methods from unimported objects, variables referenced before assignment and so on and so forth.
HTH!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With