Hacker News new | past | comments | ask | show | jobs | submit login
Bython: Python with braces. Because Python is awesome, but whitespace is awful (pypi.org)
60 points by TowerTall 8 months ago | hide | past | favorite | 89 comments



The first time I used python I was amazed.

I just wrote my code as normal (indented, thank you very much) and could ignore semicolons and curly braces, and the code just ran.

I was done in half the time!

This seems like a regression by people who are bad at indenting their code. ;-)

(see also: https://pypi.org/project/goto-statement/ )


Even without any editor features from the last 20 years (like auto-adding and auto-closing braces), typing semicolons and curly braces should not end up taking a significant portion of time.


Typing doesn't take a significant portion of a programmer's time anyway. Automation on that half of the work is only so useful.

Checking the code on your screen against your internal mental model does take varying amounts of time. It depends on language syntax and how your mind works.

Different programmers internally think in different ways, it turns out.

In my case I found that I tend to look at the indentation primarily, and then I check parentheses and semicolons against the indentation.

(So in my case python lets me skip an entire step.)


It used to depend on your keyboard layout I will say.

But nowadays it's autocompleted anyway.


a) All sorts of shitty code and text formatters will ignore or clobber whitespace.

b) When reformatting code like

   for a in x:
     f(a)
     if something:
       g()
you're liable to make a typo and commit a grave logic error.


Of course, on the other hand, you can just as easily misread this example if you don't notice the incorrect formatting.

  for (let a of x) {
    f(a);
  if (something) {
    g();
  }
  }
If you're a programmer who looks primarily at indentation for their syntax cues, it's the parens that can lead you into traps.

So there's arguments either way.

Now that I'm used to it, I do feel that indentation errors (or at least an indentation/paren mismatch) should probably be a syntax error either way.

Possibly people who argue for belts and suspenders might be on to something?


I've had a team of programmers come to me and ask why some C code wasn't working after spending some time looking at it. It was a very badly formatted piece of code and yes, they were looking at the indenting rather than the braces. This was decades ago, before the rise of modern IDE's which might make such errors obvious.

To me, that was a definitive proof that while people may prefer braces over white space the majority of those same people read code, they are using indentation to guide their mental parse tree - not the braces. One obvious reason for doing that is they may not even be able to see the braces in the editor window.

It also seems obvious that the compliers should parse the same way humans do. It makes life so much simpler when we both see the same thing, so the code the compiler is emitting matches the semantic tree we are building in our heads. Which means if your goal is correct, readable code python got it right, and just about every other language gets it wrong. The article notwithstanding, there is not a lot of wriggle on that point.


Why would curly braces stop you from making the same logic error?


They don’t. They stop it being an accident though - except in languages like C where braces are optional for single line blocks which can lead to similarly poor outcomes.

Whitespace sensitivity is the top of Python’s pretty exhaustive list of flaws.


Braces definitely do not stop accidents of this type, in my experience. All it takes is refactoring some code where you can’t feasibly count the number of open braces/parentheses (HCL tends to get like this), and misplacing a brace somewhere.


> where you can’t feasibly count the number of open braces/parentheses (HCL tends to get like this)

HCL is fine for this. Editors highlight matching braces, typically, and the parser will typically fail if they are mismatched.


> Editors highlight matching braces

Until it doesn’t fit on the page anymore so you can’t see the highlight

> parser will typically fail if they are mismatched.

The concern is that your braces are technically matched, but not in the intended way, which happens a lot. Probably more than tab errors in python in my experience


Can you give an example of HCL which is ambiguous under these conditions?


Why are you using shitty tools?


Why do I need to be good at indenting when it can be 100% automated assuming the language has braces? I’m a programmer, not a typesetter from 1520.


Indenting is not hard, and programmers should be good at it, provided they care about clean code. I like that Python forces indentation rules because it means I'll never have to read code that has confusing/improper indentation. ESLint has an indent rule to enable it for JavaScript.


Copying and Pasting code around is really cumbersome with indenting. Refactoring code is harder. Or maybe it's just me.


I am used to the curly brackets, and my keyboard muscle memory is used to the curly brackets.

I put the cursor over a curly bracket, press CTRL-M, the cursor navigates to the other curly bracket.

My eyes also parse code much faster when it is both indented and with proper brackets.

Python just makes more difficult some common tasks like commenting out a try/catch block without having to reindent all the code inside.

Not a win IMO. In fact, Python presumes it is easier to use, while in reality, it is not.


I was bitten so many times by un-indented code, thanks to copy / paste that took me hours for figure out what was the actual problem.

I wish Python had closing...how do we call them, tags(?), clauses?

Anyway, here's an example of what I mean:

Before:

    def IsPalindrome(s) -> bool:
        for i in range(len(s)):
            if s[i] != s[len(s)-1-i]:
                return False
        return True

    print("Is Anna a palindrome?", IsPalindrome('ANNa'.lower()))

after:

    def IsPalindrome(s) -> bool
        for i in range(len(s))
            if s[i] != s[len(s)-1-i]
                return False
            endif
        endfor
        return True
    enddef

    print("Is Anna a palindrome?", IsPalindrome('ANNa'.lower()))


I like these end clauses but they can get cumbersome in heavily nested control structures. Ruby, I'm looking at you.


Some editors display permanent indentation lines which help visually resolve the indentation


I prefer this idea, but instead of a shell-style ending unique to every scoped structure, maybe just a ";" character?:

    def IsPalindrome(s) -> bool
        for i in range(len(s))
            if s[i] != s[len(s)-1-i]
                return False;
        ;
        return True;
I'd also be inclined to skip it for 'def' as well, since they tend not be be as nested.


why not braces then?


They either don't line up vertically, or take extra vertical space.


...what?


The braces.


What is so wrong about properly indenting your code that you feel it's necessary to add {} to make it better?


There is nothing wrong with properly indenting code. The lack of {} makes python very ugly when things no longer fit on one line. Because Python has an opinion about whitespace there are certain ways one can break a line an other ways one cannot. The result looks very ugly. Maybe the old way of using a backslash was not the worst, but linters nowadays don't like that anymore. Also having : also does not make things look better. Python must be one of the most ugly looking languages out there.


You might be already aware of this, but besides using the backslash, the other way you can allow an expression to span multiple lines in Python is to enclose it in brackets. I find this to be pretty unobtrusive. Often your line break will already be within a pair of brackets anyway.


> when things no longer fit on one line

That sounds like a PEP-8 violation ;)

All your lines should be under 79 characters.

Might look ugly to some but to others it looks fine.


Black can usually reformat code pretty well.


Being able to copy-paste code with every editor and not ending up with broken code is a real time saver.


Why have I never had a problem copy/pasting Python?

What are y'all doing??


So if this became popular you'd have some camps using it and some not. Wouldn't that be ironicly more annoying than the problem it's solving?


No, since the conversion is trivial in both directions and a plugin could do it automatically.


Only if the use of a stupid editor is saving you time.


I don't get it either. I mostly use C#, but I somehow manage to properly indent it anyway. Whenever I use python, I don't have any issues with indentation.

To me this discussion always sounds a bit like people complaining they can't find any brown underwear.


You could always ask Apple.

Braces are explicit, whitespace implicit.

https://embeddedgurus.com/barr-code/2014/03/apples-gotofail-...


In Python, whitespace is explicit, so actually, the bug you just posted hadn't happened if they used Python. Indeed, in your link, the braces are implicit too.


And of course, "Explicit is better than implicit"

https://en.wikipedia.org/wiki/Zen_of_Python#Principles


Properly indenting code is fine. Now try modifying that code, like removing an if statement inside somewhat nested code and trying to ensure that everything still has the same meaning as before. With Python, you can never be sure you did it right.


I don't see why not. Simple visual inspection confirms if it does what you expect, unlike languages that use braces.


Because everyone (and all their editors) on every project have to handle indentation identically or as soon as someone makes an edit, things won't run.


For my ability to read and assess source code significant white space kills me.


The "properly" part.


Amazingly, Scala has recently adopted significant whitespace [1], and is moving in the opposite direction.

If you can call 2021 recent, that is.

[1] https://docs.scala-lang.org/scala3/reference/other-new-featu...


IDK; Haskell normally uses significant whitespace, but also can use braces instead, so you can write any snippet linearly.

Won't fly with Python's "one way to do it" principle though.


An interesting note about braces is how US-keyboard centric they are. Typing a braces in many keyboard layouts requires 3 keys presses (or AltGr)

Not having braces makes python very easy to teach where the keyboard layouts do not favour those characters accessibility.


Yes, but lists, dicts and comments still requires reaching altgr nevertheless.


True. But I would say their use is one order of magnitude less than a block delimiter.


  if __name__ == "__main__" {
      print_message(10);
  }
This just looks wrong without parenthesis around the conditional statement.


The purpose of parenthesis is too let the language know when the condition ends, and the statement begins. In a language with mandatory curly braces, the brace does that job already.


I’ve got a side project called Parenthon that I’m going to release soon. Let me know if you want in on the beta!


It does, doesn't it? I wish there was some sort of compat layer which would allow to natively use python libraries in Javascript



Welcome to rust!


Huh? Rust, Go, Swift and probably many more languages does the same.


In pascal you can call function that has no arguments without parentheses: writeln;

if x = 5 then writeln;


I first thought this was trolling, but reading some reactions here and there it's probably not.

I love how concise and clean Python looks. I would rather see other languages removing brackets.


Can we also have multi line lambdas? It will make loops in trees of components (html, UI etc) easier. Generating html in Python will look much better than:

``` with lib.html(): with lib.body(): with lib.ul(): for i in items: with lib.li(): lib.add(i) ```


That lib looks very useful in and of itself! Are you thinking of a particular one?


That was pseudocode. There are tons of libraries like https://pypi.org/project/dominate/


This unironically makes Python seem more cosy to me. It just feels better. My unpopular opinion, anyway.


Not sure it's really that unpopular. I personally find braces harder to read. Perhaps they are more explicit, but it just adds clutter. Compared to C and Java (even well formatted) I'd rather read someone else's Python code any day.


    $ python3 -c 'from __future__ import braces'
      File "<string>", line 1
    SyntaxError: not a chance
    $


People who don't like significant whitespace are just people who don't use a proper text editor / IDE. A lot of them think you have to manually insert the whitespace. It's the same with people who complain about parentheses in lisp.


The programming equivalent of "First world problems"


Like almost literally everything else on HN? Including complaining about it..


Sigh. I miss significant whitespace when I use curly-brace languages. I've got used to it but that's mainly Stockholm Syndrome.


If you want braces and hate whitespace, just use another language that has braces and ignores whitespace.


If you do ML, that’s a pretty tough ask


Use Python then.


I don't think I've ever craved braces in Python. I've had a few bugs due to indentation but that happens with braced languages too. If this is your preference just careful that Bython is supported into the future that you expect to use it.


If you really want to play with this, this fork is much better than the original: https://github.com/filipefjn/bython


It almost got there... It should use Begin and End instead. It could then be called PaYthon.

Seriously, let's just prohibit any of the tab characters(^I,^K,^L) in source code.


Algol 60 and Pascal had 'BEGIN ... END' to handle multiple statements in a block, but Ada and Wirth's successors, Modula-2 and Oberon, removed them except to denote where code begins in a PROCEDURE. It is much cleaner.

Poorly contrived example in Oberon:

  IF n > 1 THEN 
    n := n * n;
    RETURN 0
  ELSE 
    RETURN 1
  END


Surely you don’t mean tab and vertical tab, so you must be talking about tab and… space? How would a language with no spaces look like? How do you separate tokens?


ASCII Unit Separator, 0x1F. Obviously.

Or more seriously like any of the existing slightly more sane languages that don't need spaces, like the Lambda Calculus.


Tabs are different to normal spaces though, so it would use 2 or 4 spaces instead of a single tab.


No, the tab is a wonderful invention and should be celebrated.


Yes, Tabs are great for filling out the same forms in Triplicate every day, if you're into old timey workflows.


It need not be braces. Maybe the 'end' in Ruby, Pascal, Matlab or partially in Basic would look more natural.


I’d much rather use Hy, to be honest. at least parentheses hark from a more civilized time :)


I love this. I don't think it will ever catch on, but I love it.


Why do you need this? You can just

from __future__ import braces


I fell for it so others don't have to ;-)

  >>> from __future__ import braces
    File "<stdin>", line 1
  SyntaxError: not a chance


my humble opinion: this shouldnt be "Bython", it should be Python 4.0


could be optional so Python would be easier to pick up by people migrating from curly braces


C without the parenthesis.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: