top | item 41679290

(no title)

mg74 | 1 year ago

The number one thing I wish was addressed in future version of Python is the semantically significant whitespace.

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.

discuss

order

dbrueck|1 year ago

Whitespace is semantically significant in nearly all modern programming languages, the difference is that with Python it is completely significant for both the humans and the tools - it is syntactically significant.

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. ;-)

ec109685|1 year ago

The parent’s point was that you don’t have to care about white space when composing code in other languages since the LSP can auto format. So you could theoretically never press return or space more than once, and always have perfectly correctly functioning and formatted code at the end.

saagarjha|1 year ago

Never commented out a loop or a condition, have you?

alfiopuglisi|1 year ago

The day your wish is fullfilled is the day I stop working with Python. I can't stand all those useless braces everywhere, why are they there at all since good practice mandates proper indentation anyway?

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.

eviks|1 year ago

Single quotes are also easier to type on the default layout, no Shift

mg74|1 year ago

No need for braces. Just add "end" for marking block ending to match the already block starting keyword ":".

stavros|1 year ago

I don't have this problem, and I've been writing Python for more than twenty years. Sure, I may have the occasional wrong space somewhere, but it's maybe a few times a month, whereas I'd otherwise have to type "end" for every single block.

mg74|1 year ago

I dont think this is a problem anymore in todays world of LSPs and auto formatters. I almost never have to type "end" in Elixir for instance, it is always autocompleted for me.

pansa2|1 year ago

    >>> from __future__ import braces
    SyntaxError: not a chance

mg74|1 year ago

Thank you, but I rather not inject a tool that hasn't been updated in 6 years into my build chain. Thats how we do things in the Javascript world and frankly it sucks.

marliechiller|1 year ago

This suggestion gives me chills. I literally never face this issue. Are you using vim? Theres autoindent and smartindent features you can enable to help you.

mg74|1 year ago

Neovim + ruff lsp. I have gone through many formatters to try and get this better, but it is always worse than any other language where whitespace is not semantic.

mixmastamyk|1 year ago

Expecting formatters to fix your broken blocks is a false economy with Python and smart indentation. It takes fewer keystrokes to write Python correctly, than to add delimiters so you could write it incorrectly.

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.

ptx|1 year ago

Maybe it would help to think of "enter" as "semicolon" (since it's the statement terminator) and configure a macro or shortcut in your editor to act as "end" which decreases the indentation level by one step.

Wouldn't that make it behave pretty much as what you expect?

fluorinerocket|1 year ago

Bracket free-ness is still my favorite part of python