[Python-Dev] A grammatical oddity: trailing commas in argument lists.
Terry Reedy
tjreedy at udel.edu
Fri Jul 9 21:31:33 CEST 2010
On 7/9/2010 10:40 AM, Mark Dickinson wrote:
> While looking at a parser module issue
> (http://bugs.python.org/issue9154) I noticed that Python's grammar
> doesn't permit trailing commas after keyword-only args. That is,
>
> def f(a, b,): pass
>
> is valid syntax, while
>
> def f(*, a, b,): pass
>
> is not. I was just curious whether the latter was deliberate or an
> oversight. And if an oversight, is it worth fixing after the
> moratorium expires? (See also http://bugs.python.org/issue2009.)
The way I read the grammar in the function definition section of the
manual, a trailing comma is only allowed after a 'defparameter' that
does not come after a *. In this sense, the above is not a bug.
However, while the grammar in the call doc does not allow trailing
commas, the following note says "A trailing comma may be present after
the positional and keyword arguments but does not affect the semantics."
This has the combined effect
>>> def f(*,a): pass
>>> f(a=1)
>>> f(a=1,)
>>> def f(*,a,): pass
SyntaxError: invalid syntax
This violates the important principle that allowed def and call arg
sequences should match to the extent sensible and possible. In this
sense, the SyntaxError is a bug. So I would fix this now for 3.2 and
notify the other implementors.
--
Terry Jan Reedy
More information about the Python-Dev
mailing list