Trailing comma (was: Re: structure in Python)

Alex Martelli aleaxit at yahoo.com
Tue Oct 21 18:43:43 EDT 2003


Skip Montanaro wrote:
   ...
> Also, if the list is not a list of string constants there's no real harm
> in omitting it either, because if you extend or reorder the list and
> forget to insert a comma in the right place you'll get a SyntaxError the

Unfortunately there are lots of cases in which juxtaposing two
would-be items gives different results than separating them with a
comma -- e.g. i could have tuples with callable and args:
(
  f,
  (),
  g,
  (),
  h
  (),
  k,
  ()
)

oops, forgot comma after h, so h is called then and there rather
than stashed in the tuple for later processing.  Yeah, eventually
you'll probably get an error, but tracking it down will be useless work.

Number literals are another example, if the one before which
you forgot the comma had a sign.

And what about lists?
[
  [1],
  [0],
  [2]
  [0],
  [3],
  [1]
]

Oops, guess what, the third "sub-list" isn't because the fourth sublist
became an index into it instead -- darn, a missing comma.

It's NOT just string literals, alas.

DO use the "redundant" final comma, and live a happier life:-).


Alex





More information about the Python-list mailing list