r/Python • u/sasson10 • Apr 29 '26
Discussion Which indentation style do you all prefer for expanding brackets?
I just got curious as to what the more popular style of indentation is when expanding brackets into multiple lines.
This applies to everything, but I'll use a list as an example, the base list will be l = [item1, item2, item3]
- Going down a line and 1 indent level forward from the very first time item
=
- Keeping the first item on the definition line, then adding spaces so everything lines up
=
These are the main 2 I've seen in my time, if you have any others I'd be interested to see them as well
10
Upvotes
136
u/TheBB Apr 29 '26
The first one, which is also the one black or ruff will use.
67
u/eavanvalkenburg Apr 29 '26
The first one with a comma at the end of item3 is what I would always use (unless it fits on one line), the reason is that that gives you much cleaner diffs when committing, while the second one will be two lines changed, this is also the reasoning why tools like Ruff use that style.
39
u/differentiable_ Apr 29 '26
Idk, just let ruff or black run on pre-commit.
36
u/Noobfire2 Apr 29 '26
Don't ever come up with your own formatting rules. Every project in existance should just use
rufffor formatting and if absolutely necessary, adjust rules slightly to your preference.It will format to the first variant, btw.
5
u/CIS_Professor Apr 29 '26
PEP 8 suggests this (my preference):
or this:
4
u/Specialist_Solid523 Apr 30 '26
You forgot to include:
3
u/aminoy77 Apr 29 '26
Style 1 every time. The closing bracket on its own line means you can add/remove items and the diff only shows the actual change, not a closing bracket moving around.
Style 2 breaks the moment you rename the variable — suddenly all the alignment is wrong and you're fixing whitespace instead of code.
5
u/RevolutionaryRip2135 Apr 29 '26
I am not a psychopat so no. 1 as it is readable and two newlines cost next to nothing - use ctrl f12 to navigate structure of file in idea
Also comma after 3 so I can add new item and reorder freely.
2
u/billsil Apr 29 '26
Depends on the length. For long lists, I’ll put ~80 characters on the line. For absurdly long lists/strings, I’ll just put it on one line. I’m clearly not going to read it, so I don’t want it to take up 1000 lines.
I always put a comma after the last item though. It avoids bugs when you add a new row.
2
u/ottawadeveloper Apr 29 '26
I like (1) for all iterables because it's easy to add or remove items.
I use (2) for function parameters because it feels so weird otherwise.
2
u/karius85 pip needs updating Apr 30 '26
First. The second is terrible.
3
u/mireqB Apr 29 '26
3
u/RockOnTheWater Apr 29 '26
The first one but with trailing commas
3
u/Alkanen Apr 29 '26
Black. That is all.
2
u/big_data_mike Apr 29 '26
The first one. Easier to comment items out of the list for when I rerun the code
2
u/usrlibshare Apr 29 '26
Simple answer: I don''t give a damn.
Whenever I save my file, the auto-formatter runs, and takes care of this. I have more important things that occupy my mindspace.
gofmtstarted this for me. Discussions about this or that formatting are pointless. It's literally bikeshedding.1
u/DoubleAway6573 Apr 29 '26
the one that reduce lines in a diff for quicker commits reviews.
1
u/Beanesidhe Apr 30 '26
(1) but I am not going to waste energy on fighting anyone or anything using (2) - or other variants - about formatting preferences.
1
u/Gnaxe May 02 '26
lis not a good variable name. It's too easily confused withIand1.The style you most commonly see in Python is like,
Note the trailing comma. This is how Black would do it.
Last I checked, indenting the closing bracket is also PEP-8 compliant:
This one feels more like Lisp. It's readable, but not typical in Python:
There's also Haskell/ML style I see sometimes:
This seems weird at first, but makes sense when you consider that lines should break before a binary operator. This makes less sense in Python given that it allows a trailing comma though.
If you're not specifically trying to be terse to fit on a slide or something, just let Black do the formatting for you, and follow PEP 8 style for the rest.
1
u/saucealgerienne May 04 '26
Just install ruff with the auto format on save and you'll never have to worry about it again
1
u/shinitakunai Apr 29 '26
Two, I don't like having unneeded extra lines in the code. Been using it 16 years (so yeah fighting lot of people and linters 🥲)
1
u/GameCounter Apr 29 '26
Use ruff or black.
#1 but include the trailing comma. It makes adding a new item at the end of the list a one-line diff instead of two.
0
u/knightress_oxhide Apr 29 '26
auto format everything before a new commit
0
u/JamzTyson Apr 29 '26
When contributing to a project: Whichever style the project uses.
When using a code formatter (such as black): Whichever the formatter gives me.
For my own projects when not using an auto-formatter: I prefer the second.
0
u/quantinuum Apr 29 '26
Don’t choose formatting style because (manual) formatting isn’t something you should be doing. Get ruff or black in.
-1
u/assumptionkrebs1990 Apr 29 '26 edited Apr 29 '26
If a list (its generator) can not be written in one line (which I prefer if ever possible) I use style 2, I don't see the appeal to use two lines just for the brackets. Maybe a mix of 1 and 2 if it makes it more clear where the list is closed so:
But normally I use loops either in line or classical with append.
-6
u/gfranxman Apr 29 '26
I dunno, I can’t remember the last time I wrote or formatted code manually.
0
u/gahbageked Apr 29 '26
I just use black or some other form of code formatter that follows popular language conventions. I don't want to think about how to format code, I want to use that time to think about more important things.
In a professional setting, this is even more important. If you're working on a codebase with a team, it's much easier to use a formatting tool than waste time debating on personal stylistic preferences of code formatting. This also works to rein in the psychopaths who don't format or arrange their code at all (they exist).
0
u/TheMcSebi Apr 29 '26
First one, but I usually type the comma after the last item as well
-2
u/Balzac_Jones Apr 29 '26
This'll probably get me no end of hate, but I like a mix of the two:
My brain has always disliked the closing parentheses/bracket outdented on the line below.
-2
u/inspectorG4dget Apr 29 '26
My preference is this:
This way, adding new elements doesn't include the line with the closing bracket as a changed line in the fit diff.
I also use the same philosophy for function/method arguments, so if I'm calling a.b(1, 2).c(3, 4), aligning to the open brace/bracket/paren (rather than the standard 4-space indent) indicates which function call/scope I'm in.
This is highly opinionated and too many devs disagree with me. But for myself, I made a custome flake8 linter to catch this: https://pypi.org/project/flake8-multiline-equals/
0
u/[deleted] Apr 29 '26
[deleted] — view deleted comment