Lists And Extra Commas at end

Cameron Simpson cs at cskk.id.au
Tue Dec 24 17:12:28 EST 2019


On 24Dec2019 16:48, Avi Gross <avigross at verizon.net> wrote:
>Let me switch gears to the terminal comma situation. Unlike many 
>languages, Python decided a dangling comma is perfectly allowable in 
>many situations, perhaps all.
>
>>>> a=[1,2,3,]
>>>> a
>[1, 2, 3]
[...]
>And, of course, you can use the same dangling comma in making a tuple, 
>dictionary or set and who knows where else.
>So, is that a feature you want warnings about? After all, a dangling 
>comma may simply mean you left something out and meant to add later?

To my mind the killer argument for trailing commas is things like 
multiline lists, dicts or parameters. Example:

  def function(
      arg1=None,
      arg2=FOO,
  ):

Imagine this for a comewhat extended set of parameters. Or similar for a 
big list or dict (eg a "table" as part of a class definition).

By always including the trailing comma it is easier to insert or delete 
lines, and it reduces diff noise if you're using revision control (no 
commas flickering on and off in the diff output).

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list