Extra comma (was Re: For review: PEP 308 - If-then-else expression)

Russell E. Owen no at spam.invalid
Tue Feb 11 16:07:08 EST 2003


In article <mailman.1044738743.29728.python-list at python.org>,
 Tim Peters <tim.one at comcast.net> wrote (answering somebody who noted 
the neat ability to have a trailing comma in lists):

>...When I'm in a hurry, it's
>surprising how often I forget to add a trailing comma to the preceding item
>when appending a new item, so in containers I expect to grow I always use a
>trailing comma.  Then adding a new item usually consists of duplicating the
>last line verbatim and just editing the parts that change...
>
>For the life of me, though, I've never found a good reason to add a trailing
>comma to a formal argument list.

I second that. Python is one of the few languages I know that permits an 
extra comma at the end of lists and such. I use this all the time for 
essentially the same reasons you mention. I can safely add or delete 
items without worrying about the last one being special.

Regarding functions, I often use the following syntax, especially if the 
function has some default args:

def myFunc(
    arg1,
    arg2,
   arg3 = def,
):
    ...

There's one thing to watch out for: a ** argument cannot have any 
arguments after it, so I end up doing:
def myFunc(
  arg1,
  arg2,
**kargs):

This brings up an interesting question. Why the restriction? And why 
can't a function be called with multiple *-marked lists and/or **-marked 
dictionaries, e.g.:
foo(arg1, *argList1, *argList2)?

I realize that calling a function with * and ** args was added to mimic 
its use in function definitions, for which the "only one of each" 
restriction makes sense. But when calling a function? It'd be handy at 
times.

For lists it is unambiguous. For dictionaries one would have to define 
what happens if the same key was in both dictionaries (though that same 
issue comes up if **kargs contains a key that's already been explicitly 
listed).

-- Russell




More information about the Python-list mailing list