Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Always leave a trailing comma in Python lists, dicts, tuples (poe.com)
4 points by behnamoh on July 30, 2023 | hide | past | favorite | 7 comments


When I'm working on a function that I think I'll add more arguments to, or have a list I think I'll add more items to, I do this just so I don't have to modify the line above where I'm adding something (because I often end up forgetting the comma otherwise). It's interesting to see it pointed out that this helps with diffs.


There is a diff tool called difftastic: https://github.com/Wilfred/difftastic

The idea is that it does not show diff based on text change, but on syntastic meaning. For that, it uses tree-sitter.

I think it still shows the trailing comma in the situation as shown in the article, but it's quite different experience than the standard text based diff.


Same for kotlin, javascript, probably others.

I'm always shocked at people's resistance when I suggest this. To some people, commas are the hill to die on, and seemingly not for any reason except "that's how we've always done it".


I'd say the reason is that programming languages are based on natural languages, and in English, lists do not end with a comma.


i hope we never see a programming language add an "and" between the last and 2nd last arg.


Then we'd have the Oxford comma problem:

  [1, 2, 3 and 4]
or

  [1, 2, 3, and 4]


For the same reason, I now prefer this

    foo
    {
       ...
    }
to this

    foo {
        ...
    }




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: