[Python-ideas] Special-case 3.x 'print x' SyntaxError

Mike Müller mmueller at python-academy.de
Mon Sep 15 11:50:26 CEST 2014


Am 15.09.14 11:35, schrieb Terry Reedy:
> On 9/15/2014 4:23 AM, Chris Angelico wrote:
>> On Mon, Sep 15, 2014 at 6:12 PM, 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(...)"?
>>
>> You mean like this?
>>
>> rosuav at dewey:~$ python3
>> Python 3.4.1+ (default, Aug 18 2014, 12:06:51)
>> [GCC 4.9.1] on linux
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> print "Hello?"
>>    File "<stdin>", line 1
>>      print "Hello?"
>>                   ^
>> SyntaxError: Missing parentheses in call to 'print'
> 
> Excellent!  Someone's time machine strikes again.

+1

In general, I think the single most useful improvement for beginners are
better hints in error message about what possibly went wrong.

If this goes beyond syntax errors, adding a switch to turn this off might
be useful. Just in case somebody actually scrapes messages or your doctests
break.

Mike



More information about the Python-ideas mailing list