Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: trailing comma after a catch-all argument

Tags:

python

syntax

I am baffled by the trailing comma in a function parameter list:

def f( *args, ): pass

earns me a SyntaxError exception. In python 3, even this:

def f( *, arg = 1, ): pass

raises a syntax error exception. Both point to the closed parenthesis.

Removing the trailing comma, everything is quiet. I am scratching my head on https://docs.python.org/3/reference/compound_stmts.html#function-definitions but it is somehow beyond me (and possibly also wrong... at least, in my browser's rendering I can't pair the last closing parenthesis in the parameter_list definition). Am I doing something wrong? I am the

def f(*,
      a: "doc A" = 1,
      b: "doc B" = 2,
      c: "doc C" = 3,
      d: "doc D" = 4,
      # ... and maybe more,
     )

type of person, and this issue kind-of annoys me (maybe that is the wrong part, and I should become the , a: "doc A" = 1 type of person - but it looks weird to me).

Using python as distributed in Gentoo ebuilds dev-lang/python 2.7.12 and 3.5.2 (from http://www.python.org).

like image 469
Hamlet Avatar asked May 01 '26 12:05

Hamlet


1 Answers

Support for trailing commas following a catch-all argument was added in python 3.6.

It is not supported in 3.5 which explains your SyntaxError.

like image 122
Eric Blum Avatar answered May 04 '26 03:05

Eric Blum