[Python-Dev] A grammatical oddity: trailing commas in argument lists.

Mark Dickinson dickinsm at gmail.com
Fri Jul 9 22:26:21 CEST 2010


On Fri, Jul 9, 2010 at 8:37 PM, Dino Viehland <dinov at microsoft.com> wrote:
> Terry wrote:
>> 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.
>
> +1 on fixing it - trailing commas are awesome.  I'm always annoyed in
> C# where I frequently can't use them.  This seems like a bug fix level
> change that should be easy for the other implementations to fix.

Thanks for all the feedback.

If the grammar is changed to allow "def f(*, a,): pass", that still
leaves some more open questions:  which of the following should be
valid?

(1) def f(*args,): pass
(2) def f(**kwargs,): pass
(3) def f(*,): pass

Just for the sake of simplicity it would seem to make sense allow all
these, even if there's no obvious immediate use;  for me, it keeps the
mental footprint of the language small---I don't have to remember when
the comma is or isn't allowed.  Note that (1) and (2) aren't valid
(and never have been, as far as I know) in Python 2.x, though.
(Neither is (3), of course, since keyword-only arguments are 3.x
only.)

(3) is a bit subtle:  "def f(*): pass" is actually allowed by the
grammar, but produces a SyntaxError later on, when the parse tree is
converted to AST:

    >>> def f(*): pass
    ...
      File "<stdin>", line 1
    SyntaxError: named arguments must follow bare *

So it probably doesn't matter much whether (3) is permitted by the
grammar or not.

-- 
Mark


More information about the Python-Dev mailing list