Syntax error for simple script

Erik python at lucidity.plus.com
Tue Jun 27 15:38:46 EDT 2017


On 27/06/17 15:05, Steve D'Aprano wrote:
> On Tue, 27 Jun 2017 08:34 am, Erik wrote about the print function error message:
>> To be fair, this already seems to be a special case:
> [...]
>>   >>> len "bar"
>>     File "<stdin>", line 1
>>       len "bar"
>>               ^
>> SyntaxError: invalid syntax
> 
> Correct, the print function is singled out as a special case, because people are
> unlikely to come across tutorials telling them to write
> 
>     len "bar"

Sure. My point was that it's obviously a special case and not a generic 
error message that would occur in the same situation with any other 
callables. Hence there is obviously a single, specific place in the 
parser code that could just be changed slightly if it would help. I 
haven't looked at the source code for this.

> Indeed. But it is hardly terse: it's a full sentence:
> 
> Missing parentheses in call to 'print'

Right, but as in this case, it's likely to trip up a beginner who 
happens to be following a Py2 tutorial or copying Py2 examples from SO 
or somewhere. In fact, apart from those of us who switch between 
versions for different projects and just forget (old habits die hard), a 
beginner typing in a Py2 example is probably the only case where this 
error is seen.

So I don't understand the resistance to making the error be a bit more 
explicit about why it is being generated to help those users - it 
doesn't explain (the obvious reason) _why_ their tutorial/example gives 
them an error when it doesn't to the person who wrote the tutorial.


Note that I am *explicitly* making Py3 the "good guy" and Py2 the "bad 
guy" in all the wordings that I have given that mention a version number 
- it could even be something like "Missing parentheses in call to 
'print'. This is obsolete Python 2 syntax. Please use modern Python 3 
syntax.". This addresses ChrisA's issue with the proposed wordy error 
that he thought Py3 was playing second fiddle in that version.




One other possibility is that a beginner fires up Py3 and gets that 
"weird" (to them) error when they tried their tutorial or SO code and 
then finds out that if they fire up Py2 instead it just works. Now they 
might continue to use Py2 because it's "the one that works" for them.

I would suggest that making the error dissuade them from doing that (for 
the cost of a few tens of bytes) is a good thing.

>> I think the suggestion above is a bit wordy, but even if it was
>> something like "Missing parentheses in call to the 'print' function"
> 
> I think this is a good example of "the curse of knowledge". Its hard for expert > to think like a non-expert.

[snip]

> But to the beginner, adding "function" at the error might as well be
> 
> Missing parentheses in call to the 'print' wharrgarbl.

You are concentrating on the detail of what I wrote. I don't care what 
the eventual wording is (and for sure, someone else is far more 
qualified than me to suggest wording that is better for a beginner). My 
point is simply that the error _could_ contain _some_ more information 
that addresses this issue for a beginner who may be typing in Py2 
examples that encourages them to seek out Py3 tutorials instead.

> That's why I asked Ben if there was something we
> could do to make the sentence clearer.

Exactly. Ben is an example of someone more qualified than me to suggest 
the correct words.

>> Either that or just make it "SyntaxError: invalid syntax" like all the
>> others.
> 
> We've been there: adding the specific print message is new to Python 3.5 (or
> maybe 3.4, I forget which). Python 3.3 gives just "SyntaxError: invalid
> syntax".

That surprises me, to be honest. I had presumed that the parser had just 
had the machinery for that message placed where the 'print' keyword used 
to be handled rather than ripping the whole thing out when it became a 
function. Interesting that it's a retrospective special case.

E.



More information about the Python-list mailing list