[Python-ideas] Special-case 3.x 'print x' SyntaxError
Steven D'Aprano
steve at pearwood.info
Mon Sep 15 10:43:13 CEST 2014
On Mon, Sep 15, 2014 at 04:12:31AM -0400, Terry Reedy 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(...)"?
+1
Normally I'm a bit reluctant to include overly specific advice in
exceptions, but I think this is a common and important enough case that
if "print spam" can be distinguished from similar "foo spam" errors, we
ought to give a specific error message rather than a generic one.
> 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
> I imagine that there have been other duplicates. The same question (and
> answer) has appeared multiple times on python-list also.
And multiple times on the tutor list too.
> 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 ).
Yes. Did you really mean "print spam", or should it be print_spam? Or
print_(spam)? or prentspam? Fixing people's coding errors is not the
job of the compiler -- DWIM code is dubious at best if not outright
harmful.
http://www.hacker-dictionary.com/terms/DWIM
--
Steven
More information about the Python-ideas
mailing list