by mg74 · 2y ago · 0 comments
Python is absolutely the worst language to work in with respect to code formatters. In any other language I can write my code, pressing enter or skipping enter however I want, and then the auto formatter just fixes it and makes it look like normal code. But in python, a forgotten space or an extra space, and it just gives up.
It wouldn't even take much, just add a "end" keyword and the LSP's could just take care of the rest.
GIL and JIT are nice, but please give me end.
I've actively used Python for a quarter of a century (solo, with small teams, with large teams, and with whole dev orgs) and the number of times that not having redundant block delimiters has caused problems is vanishingly small and, interestingly, is on par with the number of times I've had problems with redundant block delimiters getting out of sync, i.e. some variation of
if (a > b)
i++;
j++;
Anytime I switch from Python to another language for awhile, one of the first annoying things is the need to add braces everywhere, and it really rubs because you are reminded how unnecessary they are.Anyway, you can always write #end if you'd like. ;-)
Most language design decisions involve tradeoffs, and for me this one has been a big positive, and the potential negatives have been - across decades of active usage - almost entirely theoretical.
Also, the real-world use case for importing code and auto formatting it is quite doable with Python - the main thing you can't do is blindly (unintelligently) merge two text buffers and expect it to work every time, but in general it takes only a smidgen of analysis on the incoming text to be able to auto reformat it to match your own. You could go all the way and parse and reformat it as needed, but much simpler/dumber approaches work just as well.
What you can't do is take invalid Python code and expect it to autoformat properly, but that borders on tautological, so... :)
There are people that actually do this?
(funny enough, that particular scenario is actually harder to miss in Python since it often produces an error that prevents the program from running at all)
some_function();
Python: some_function()
I am debugging how this function works so I actually only want it to run when some condition is true. C: if (some_condition) {
some_function();
}
Python: if some_condition:
some_function()
Oops. That's not indented correctly, so it won't run. To be fair neither actually looks good but that's 1. fine because this is for debugging and 2. for the C code I just format the file and it is instantly fixed. What if I have a loop? C: for (int i = 0; i < 5; ++i) {
iteratively_optimize();
}
Python: for i in range(5):
iteratively_optimize()
Unfortunately this breaks something so I want to step it through. In C I comment out the loop as follows: // for (int i = 0; i < 5; ++i) {
iteratively_optimize();
//}
In Python: // for i in range(5):
iteratively_optimize()
Nope, that's broken too. I can't just autoformat this code either because the formatter can't look at scope using anything else. I have to manually go and fix the indentation on that line too.These are actually very small cases. I can even imagine you saying, in C you have to fix two places if you want to comment out a loop or if: the opening brace and the closing one. In Python you need to comment out the control statement and the second thing is fixing the indentation, so what's the big deal? Well, as the number of lines in the block grows larger in C it's still just commenting out the two braces, while in Python you have to select the whole region line-perfectly and fix the indentation. As someone who writes both I always find this to be a lot more fiddly and annoying.
I am at the point where I prefer single quotes for strings, instead of double quotes, just because they feel cleaner. And unfortunately pep8 sometimes mandates double quotes for reasons unknown.
Are there any languages that use it, or is Python unique in using `:` to begin a block?
IIRC, Python's predecessor (ABC) didn't have the trailing colon but they did some experiments and found it increased readability.
>>> from __future__ import braces
SyntaxError: not a chanceAnd IMO for good reason. It makes the code so much cleaner and it's not like it particularly takes effort to indent your code correctly, especially since any moderately competent editor will basically do it for you. Tbh I actually find it much less effort than typing braces.
Python’s tradeoffs pay dividends every day, at the expense of few questions a year. Also code is read 10x more than written, where the extra delimiters lower signal to noise.
Wouldn't that make it behave pretty much as what you expect?