PEP 8 - trailing or leading commas

Recently, I came across this code someone else had written that looked like this:

logging.basicConfig(
    filename='newfile.log'
    , level=logging.INFO
    , filemode='w'
)

It looks wrong to me, but I went through PEP 8, and didn’t find any guidance on this style. PEP 8 does talk about operators at the beginning of multi-lines, but I couldn’t find anything about leading commas. I could imagine an argument for it, but it seems quite unusual and non-standard.

Did I miss a bit in PEP 8? Should this style be discouraged? Should that be in PEP 8?

Yes, it should be discouraged. No, it doesn’t need to be in PEP 8.

There are plenty of examples with commas in PEP 8, none have leading commas.

There’s a section on when to use trailing commas, we don’t need one about leading commas.

3 Likes

Under Pet Peeves, there is a mention to “Avoid extraneous whitespace […] Immediately before a comma.” That teeeechnically implies that you shouldn’t use leading commas (which necessarily have whitespace before them in the form of a newline/indentation…)

2 Likes

Leading commas are a quirk used in languages that don’t allow trailing commas. You can add another line to the function call and still have a one-line diff.

Python allows trailing commas, so we get the benefit without tortured typography:

logging.basicConfig(
    filename='newfile.log',
    level=logging.INFO,
    filemode='w',
)
12 Likes

It’s a monstrosity that’s common in SQL code. I believe the logic is that it makes adding lines easier (somehow) when final trailing commas aren’t allowed. There’s no reason for it in Python, and it should be discouraged in Python code. Personally, I don’t think it needs to be in PEP 8, it’s covered by the fundamental rule of “don’t write ugly code”.

Since when did style guides become “if it’s not explicitly discouraged, it’s permissible”?

2 Likes

I’ve also seen it in JSON. (And it’s a monstrosity there too.)

SQL and JSON both forbid trailing commas.

Here’s a fun Tim Peters anecdote. We came across this style at PythonLabs and we were all horrified. Tim qualified in a slightly off-color joke, saying “No matter how you shake and dance…” and a huge grin.

5 Likes

I take it from the sentiment in this thread there’s little point in me proposing the allowance of a leading-comma so we can have a consistent:

logging.basicConfig(
    , filename='newfile.log'
    , level=logging.INFO
    , filemode='w'
)
3 Likes

It would make for slightly cleaner diffs (only one line added rather than 1 deleted 2 added) but I personally don’t agree with slightly cleaner diffs at the expense of significantly uglier code lol

1 Like

Hmmm, not a fan. :nauseated_face:
I prefer the comma on the end of every line. That makes it slightly easier to add extra lines, (that is, in this case, further (long) keyword arguments), and also sort a block of lines (more relevant for a list of items).

1 Like

Sorry, but I couldn’t resist…

The code I’ve just read is one of the most insanely idiotic things I have ever seen. At no point in its rambling, incoherent syntax was it even close to anything that could be considered a rational code style. Everyone on this thread is now dumber for having looked at it. I award it no points, and may God have mercy on its soul.

3 Likes

It makes adding lines easier at the end, but harder at the beginning.
Not a fan myself, I don’t think it’s a net win overall.