0

Let f be a function with 2 arguments. I do not really understand why the syntax:

f(1,2,)

actually works (note the ending comma)? It looks like the arguments are seen as a tuple, but in this case, if g is a function with 1 argument, why g(1) is working (instead of g(1,) in the same way we have to write (1,) to get the tuple with only one element)?

I think it's a Python's specification question but I do not know where to search.

Sebastien
  • 682
  • 6
  • 13
  • I'm going to need you to reword this. I'm not exactly sure what you are asking. The positional arguments are a list `args*`. neither `f(1)`, or `f(1,)` should not work for a function definition that specifies two arguments. You should see `f() takes exactly 2 arguments (1 given)` – nsfyn55 Apr 15 '14 at 16:27
  • although `l = [1,2,]` is a valid list assignment – nsfyn55 Apr 15 '14 at 16:29
  • 1
    The grammar for calls is described [here](https://docs.python.org/3/reference/expressions.html#grammar-token-call). – Kevin Apr 15 '14 at 16:32
  • I've reformulated: I'm not interessed in the arg* or key** arguments, but in the way the classical args are represented. Must I understant from your last answer that the arguments are indeed list ? – Sebastien Apr 15 '14 at 16:32
  • Ok thanks @Kevin . Is there any deep reason that make the spec allows this trailing comma? – Sebastien Apr 15 '14 at 16:41
  • I don't see any explanation in the documentation, but my guess is: it makes it easier to make changes to function calls that appear over multiple lines. For example, if you want to change the order of arguments of `f( [newline] 1, [newline] 2, [newline])`, then you only need to cut one line and paste it in its new position. If trailing commas were disallowed, you would have to cut, paste, and delete/retype commas in the appropriate spots. – Kevin Apr 15 '14 at 16:44
  • A lot of the grammar and pep8 related items are geared to making editing easier. If you are using `vim` or another editor that makes cutting and pasting entire lines easy. This can be a pretty big time saver because it requires several keystrokes to add or remove the comma. – nsfyn55 Apr 15 '14 at 17:22
  • Ok ! Thanks ;p I would like to "+1" your answers but it appears that I don't have enough reputation :(. – Sebastien Apr 15 '14 at 18:34

0 Answers0