DEV Community

loading...
Cover image for Would you use python with braces

Would you use python with braces

codemonkey51 profile image Codemonkey51 ・1 min read

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()
Enter fullscreen mode Exit fullscreen mode

Would become

def hello(): # {
    print("Hello, World!")
# }
hello()
Enter fullscreen mode Exit fullscreen mode

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)

pic
Editor guide
Collapse
patarapolw profile image
Pacharapol Withayasakpunt • Edited

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.

Collapse
shadowtime2000 profile image
shadowtime2000

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.

def hello() {
    print("Hello, World!")
}
Enter fullscreen mode Exit fullscreen mode
Collapse
codemonkey51 profile image
Codemonkey51 Author

I think there was a project/repo before that changed pythons grammar to use braces, I can see if I can find it again.

Collapse
tylerlwsmith profile image
Tyler Smith

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.

Collapse
codemonkey51 profile image
Codemonkey51 Author

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.

Collapse
tylerlwsmith profile image
Tyler Smith

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.

Thread Thread
codemonkey51 profile image
Codemonkey51 Author

I should have clairified it more in the post (I just added that in an edit). I see your point about the semi-colons.

Collapse
drpaneas profile image
Panagiotis Georgiadis

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

Collapse
codemonkey51 profile image
Collapse
chrisgreening profile image
Chris Greening

Check out from __future__ import braces

Collapse
codemonkey51 profile image
Codemonkey51 Author

Yes ofc, it is a great feature. Everyone should try it

Collapse
stevezieglerva profile image
Steve Ziegler

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!

Collapse
codemonkey51 profile image
Codemonkey51 Author

I see your point. Thanks for the feedback!

Collapse
codemonkey51 profile image
Codemonkey51 Author

Any feedback on my writing style would be greatly appreciated

Collapse
lechatthecat profile image
lechat • Edited

We can make new language that has braces. I sometimes work on my toy project that has braces: github.com/lechatthecat/oran

Collapse
rhinocodes profile image
sudo killall windows

github.com/mathialo/bython This project adds braces to Python