Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's look at the <a href="http://docs.python.org/2.7/reference/expressions.html#calls" rel="nofollow noreferrer">language specification</a>:</p> <pre><code>call ::= primary "(" [argument_list [","] | expression genexpr_for] ")" argument_list ::= positional_arguments ["," keyword_arguments] ["," "*" expression] ["," keyword_arguments] ["," "**" expression] | keyword_arguments ["," "*" expression] ["," "**" expression] | "*" expression ["," "*" expression] ["," "**" expression] | "**" expression positional_arguments ::= expression ("," expression)* keyword_arguments ::= keyword_item ("," keyword_item)* keyword_item ::= identifier "=" expression </code></pre> <p>Let's sift down to the parts we care about:</p> <pre><code>call ::= primary "(" [argument_list [","]] ")" argument_list ::= positional_arguments ["," keyword_arguments] ["," "*" expression] ["," keyword_arguments] ["," "**" expression] positional_arguments ::= expression ("," expression)* keyword_arguments ::= keyword_item ("," keyword_item)* keyword_item ::= identifier "=" expression </code></pre> <p>So, it looks like after any arguments to a function call, we're allowed an extra <code>,</code>. So this looks like a bug in the cpython implementation.</p> <p>Something like: <code>f(1, *(2,3,4), )</code> should work according to this grammar, but doesn't in CPython.</p> <hr> <p>In an earlier answer, <a href="https://stackoverflow.com/users/102441/eric">Eric</a> linked to the <a href="http://docs.python.org/2/reference/grammar.html" rel="nofollow noreferrer">CPython grammar specification</a>, which includes the CPython implementation of the above grammar. Here it is below:</p> <pre><code>arglist: (argument ',')* ( argument [','] | '*' test (',' argument)* [',' '**' test] | '**' test ) </code></pre> <p>Note, that this grammar is <strong>not the same</strong> as the one proposed by the language specification. I'd consider this an implementation bug.</p> <hr> <p>Note that there are additional issues with the CPython implementation. This should also be supported: <code>f(*(1,2,3), *(4,5,6))</code></p> <p>Oddly though, the specification does not allow <code>f(*(1,2,3), *(4,5,6), *(7,8,9))</code></p> <p><strong>As I look at this more,</strong> I think this part of the specification needs some fixing. This is allowed: <code>f(x=1, *(2,3))</code>, but this isn't: <code>f(x=1, 2, 3)</code>.</p> <hr> <p>And to perhaps be helpful to the original question, in CPython, you can have a trailing comma if you don't use the <code>*args</code> or the <code>**kwargs</code> feature. I agree that this is lame.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload