[Python-ideas] Special-case 3.x 'print x' SyntaxError
Nick Coghlan
ncoghlan at gmail.com
Mon Sep 15 11:41:17 CEST 2014
?
On 15 Sep 2014 18:13, "Terry Reedy" <tjreedy at udel.edu> wrote:
>
> One of the problems with new Python programmers using 3.x is that they
first read 'print x' in 2.x based material, try 'print x' in 3.x, get
"SyntaxError: invalid syntax" (note the uninformative redundant message),
and go "huh?" or worse.
>
> Would it be possible to add detect this particular error and print a more
useful message? I am thinking of something of something like
> SyntaxError: calling the 'print' function requires ()s, as in "print(x)"
> or maybe
> SyntaxError: did you mean "print(...)"?
>
> I was 'inspired' by a recent SO question
>
https://stackoverflow.com/questions/24273599/idle-gui-is-unable-to-give-output
> which was closed as a duplicate of the 2009 question
>
https://stackoverflow.com/questions/826948/syntax-error-on-print-with-python-3
Note my (relatively recent) comment there pointing to this self-answered
question:
https://stackoverflow.com/questions/25445439/what-does-syntaxerror-missing-parentheses-in-call-to-print-mean-in-python/
With any luck, searching for the more specific error message will bring
folks directly to that page.
That change will ship with 3.4.2 in a couple of weeks time. (It was one of
the changes we implemented based on the post-PyCon feedback regarding
barriers for newcomers)
> If we do this, I am sure someone will ask why we do not automatically
'fix' the error. One answer would be that the closing ) is needed to
determine the intended end of the call. A longer version would be that if
we insert (, we are just guessing that the insertion is correct and we
still would not know, without guessing, where to put the ).
Implicit call statements are fully implementable within the constraints of
CPython's parser (I wrote a proof of concept last year and posted it on the
tracker).
Guido isn't a fan though, and proving it was technically possible was
enough to scratch my own itch. If someone else wants to dig up that patch
and work it up into a full PEP, feel free - just be prepared to be very
responsive to feedback regarding the genuine readability concerns with the
idea, as well as for the chance it may still get rejected regardless :)
Cheers,
Nick.
>
> --
> Terry Jan Reedy
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140915/1cdc5373/attachment-0001.html>
More information about the Python-ideas
mailing list