It came up today as a real surprise to me. Up until then, I thought that long gone were the times when I stared at plain old syntax errors in confused bewilderment. Well, at least if we’re talking languages I have some experience with, like Python. So when it happened to me today, I was really caught off-guard.
The crux of the issue can be demonstrated in the following, artificial example:
The goal is to build some simple XML tree using the most convenient interface, i.e. the
lxml.builder.E manipulator from the
lxml library. The real code is somewhat longer and more complicated but this snippet encapsulates the issue pretty neatly.
And strange as it may seem, this little piece produces a
SyntaxError at the final closing parenthesis:
In such case, the first obvious thing anyone would do is of course to look for unmatched opening brace. With the aid of modern editors (or even not so modern ones ;>) this is a trivial task. Before too long we would therefore find out that… all the braces are fine. Double-checking, just to be sure, will have the same result. Everything appears to be in order.
But, of course, we still have the syntax error. What the hell?!
As it turns out, the offending line is just above the seemingly erroneous parentheses. It’s this one:
Or, to be more specific, it is the very last character of this line that the interpreter has problems with:
See, Python really doesn’t like this trailing comma. Which, admittedly, is more than surprising, given how lenient it is in pretty much any other setting. You may recall that it’s perfectly OK to include the additional comma after the final element of a list, tuple, or dictionary, and it is quite useful to do so in practice. Not only that – it is also possible for argument lists in function call. Indeed, this very fragment has one instance of such trailing comma that appears after a keyword argument (
But apparently this doesn’t really work for all kinds of arguments. If we unpack some positional ones (using
* operator), we cannot put a comma afterwards. The relevant part of Python grammar specification is stating this, of course:
but I wouldn’t call it very explicit. And it seems that you actually can have a comma after
*foo but only if another argument follows. If my intuition of formal grammars is correct, the reason for this rule to prohibit
foo(**kwargs,) for that matter) is strictly related to the fact than Python’s grammar is LL(1). And this, by the way, is here to stay. Quoting PEP 3099:
Simple is better than complex. This idea extends to the parser. Restricting Python’s grammar to an LL(1) parser is a blessing, not a curse. It puts us in handcuffs that prevent us from going overboard and ending up with funky grammar rules like some other dynamic languages that will go unnamed, such as Perl.
I, for one, deem this attitude completely reasonable – even if it results in 20 minutes of utter confusion once in a blue moon.
Footnote: The title is of course a not-so-obvious reference to The Infernal Semicolon.
I recently got a bulldozer to the face when I found what the difference between
localVar = complexFunction(lotsaArguments)
localVar = complexFunction(lotsaArguments),
is. I can give you a hint – a syntax error would be a blessing. Considering it was part of a complex, 2-hours-long multi-threaded exception-ridden system test… well… things only got worse.
Hah. Seems like Python is nice, clean and dandy – as long as you remember to put only the right amount of commas :)