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

Terry Reedy tjreedy at udel.edu
Fri Jul 9 23:59:58 CEST 2010


On 7/9/2010 4:26 PM, Mark Dickinson wrote:
> 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

I would follow the same principle of making def and call match. Currently
 >>> def f(*args,): pass

SyntaxError: invalid syntax
 >>> def f(*args): pass

 >>> f(*(1,2,3),)
SyntaxError: invalid syntax

Giving the call doc "A trailing comma may be present after the 
positional and keyword arguments but does not affect the semantics.", I 
was not really expecting the second syntax error. Same behavior for f(**k,).

So if you allow the definition, allow the call too.

> 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.

Agreed.

-- 
Terry Jan Reedy



More information about the Python-Dev mailing list