Re:Black – Uncompromising Python code formatter
by zild3d on Friday May 17, 2019 @12:51PM
(#19941342)
why not use their ```# fmt: off``` ?
I have not used Black, but considering it so I am curious about these occasional situations where you want to override auto-formatting
Re:Black – Uncompromising Python code formatter
by arnioxux on Friday May 17, 2019 @10:44PM
(#19945273)
I write a lot of scientific code with python and in those cases you really want your equations and matrix columns aligned for readability. These matrices appear so often adding fmt:off destroys the readability/elegance gain from the clever formatting.
Here's an example snippet by Peter Norvig that is beautifully formatted:
def neighbors4(point):
"The four neighboring squares."
x, y = point
return ( (x, y-1),
(x-1, y), (x+1, y),
(x, y+1))
def neighbors8(point):
"The eight neighboring squares."
x, y = point
return ((x-1, y-1), (x, y-1), (x+1, y-1),
(x-1, y), (x+1, y),
(x-1, y+1), (x, y+1), (x+1, y+1))
Re:Black – Uncompromising Python code formatter
by lysium on Friday May 17, 2019 @02:02PM
(#19942016)
There is #format off for these few cases.
Re:Black – Uncompromising Python code formatter
by hrktb on Friday May 17, 2019 @02:15PM
(#19942126)
I don’t code in python so have no opinion on Black, but most code formatter can be adjusted to exclude some cases of formatting.
I you would be fine with 95% of what it does and just want it to ignore the 5% where you care the most, I think the tool could benefit you.
At least that’s how I feel about autoformatting in general.
In the example I mentioned, I may sometimes choose to put a small dictionary initialization into a single line if it's just a detail, or may split it into multiple lines if it's important enough for someone reading the code to pay attention to it before going further.
This simply cannot be decided by an automatic formatter without it knowing the code's purpose which, for now, requires a human brain.
Someone mentioned elsewhere that automatic formatters remove the cognitive load of formatting code, but I argue that load is an intrinsic part of programming, because it is (for the most part) the effort of communicating to the next person that comes along.