One of pythons key fundamentals is using whitespace instead of braces. This can help readability, but make coding harder sometimes.
Some people suggest "fake braces" (by using comments) for example:
def hello():
print("Hello, World!")
hello()
Would become
def hello(): # {
print("Hello, World!")
# }
hello()
But that's not good enough, it looks like braces, but it doesn't act like braces. I know that braces are against the python philosophy, and "if you want braces use js", but no. I like pythons syntax (except white space), I like its design, and typing. The only part that I would really change is that it would use braces.
This would make one liners, and obfuscation easier, it would allow better multiline lambdas, and it would still be possible to use all the whitespacing you want.
Your choice
If you could chose python as it is, or python with braces, which would you use. Reply your answer in the comments.
EDIT: this post is meant in the sense that you could chose wether python uses braces or white peace for everyone, or it is a feature included my default that can be easily enabled thru something similar to future
Discussion (16)
Why not?
Actually, my feeling is Kotlin is even more flexible than Python; but also with braces and very minimal namespace clashes. (You can skip any reserved words with "`".)
Also, whitespace is only a forced prettier process. Brace or not is another thing.
And yes, you can be even prettier - use Black.
I personally hate Python forced whitespace. I would use it with curly braces like that, but it just looks weird with forced whitespace. Maybe somebody can create a language which compiles to Python with some syntax not including whitespace.
I think there was a project/repo before that changed pythons grammar to use braces, I can see if I can find it again.
No: it's a nonstandard convention that would confuse other developers working on the project and it's not Pythonic. I'm not a huge fan of Python's significant whitespace, but if I'm going to code in Python, I want to make sure I follow the conventions of the language to the best of my knowledge.
I kind of meant this post as: if you could choose would python use braces, or if it was a choice that was widely available and built in, would you use it. And I get your point.
Ah, I misunderstood–my apologies! My answer is still "no" however: I'd rather have one blessed way of styling the code rather than the kind of semicolons vs no semicolons flame war that the JavaScript community has.
I should have clairified it more in the post (I just added that in an edit). I see your point about the semi-colons.
I would not use braces in python, so it looks like python. If you ever encounter yourself wondering if "this line is part of this or that wall of code?", then it's a good indication you need to encapsulate some stuff.
Also, there is good chance to hit any strict linter while you contribute to open-source. Braces is not pythonic, so I guess if the project has a CI pipeline, it should fail parsing your code (in case of braces).
Good points
Check out
from __future__ import braces
Yes ofc, it is a great feature. Everyone should try it
No. I like it as-is. Modern IDEs with automatic formatting and ability to collapse sections make it easy to use.
Your writing is good!
I see your point. Thanks for the feedback!
Any feedback on my writing style would be greatly appreciated
We can make new language that has braces. I sometimes work on my toy project that has braces: github.com/lechatthecat/oran
github.com/mathialo/bython This project adds braces to Python