Comma is not always OK in the argument list?!

Nick Vatamaniuc vatamane at gmail.com
Fri Jul 28 08:10:22 EDT 2006


Roman,

According to the Python call syntax definition
(http://docs.python.org/ref/calls.html) commas should be allowed, so it
seems like a minor bug.  Here are the  lines in question:
-----http://docs.python.org/ref/calls.html-----------
call ::= primary "(" [argument_list [","]] ")"
argument_list::=positional_arguments ["," keyword_arguments] ["," "*"
expression] ["," "**" expression]
	| keyword_arguments ["," "*" expression] ["," "**" expression]
        | "*" expression ["," "**" expression]
        | "**" expression
----------------------------------------------------------
If you notice in the 'call' definition, no matter what the
'argument_list' is, it can be followed by an optional ',' right before
the closing ')'. Your code is a counterexample to this. Here is a more
exhaustive example:
--------------------------------------------------------
>>> def f(a,b):
...     pass
...
>>> f(1,2)
>>> f(1,2,)
>>> f(1,*[2])
>>> f(1,*[2],)
  File "<stdin>", line 1
    f(1,*[2],)
             ^
SyntaxError: invalid syntax
>>> f(1,**{'b':2})
>>> f(1,**{'b':2},)
  File "<stdin>", line 1
    f(1,**{'b':2},)
                 ^
SyntaxError: invalid syntax
>>> f(*[1,2])
>>> f(*[1,2],)
  File "<stdin>", line 1
    f(*[1,2],)
             ^
SyntaxError: invalid syntax
>>> f(**{'a':1,'b':2})
>>> f(**{'a':1,'b':2},)
  File "<stdin>", line 1
    f(**{'a':1,'b':2},)
                     ^
SyntaxError: invalid syntax
>>>
>>> f(1,b=2)
>>> f(1,b=2,)
>>> f(a=1,b=2,)
>>>
-----------------------------------------------------

Anybody else knows more about this?
/invoking the spirit of GvR... ;-)

Nick Vatamaniuc






Roman Susi wrote:
> Hi!
>
> it is interesting that I found this syntax error:
>
> >>> a = {}
> >>> str('sdfd', **a,)
>   File "<stdin>", line 1
>     str('sdfd', **a,)
>                    ^
> SyntaxError: invalid syntax
>
>
> I just wonder is it intentional or by-product (bug or feature)?
> (The behaviour makes sense, of course... I tend to leave commas when I
> expect a list to be added to (i.e. almost always ;-)))
> 
> 
> Regards,
> Roman




More information about the Python-list mailing list