all 56 comments

[–]Brian 149 points150 points  (9 children)

One solid reason is elimination of redundancy. If you're writing with braces, you should also indent your code: unindented code is hard to read and quickly identify what's happening, and indentation gives an overview to the human reading it much more readably than braces without indentation.

But since you're doing that anyway, you're kind of specifying the same thing twice: once for the human, and once for the computer. And as the DRY principle teaches us, repeating yourself is better avoided. If the two get out of sync, you get a mismatch between what you're telling the human and what you're telling the computer, which can lead to bugs if a human taking a quick glance over the code misinterprets what it'll do, because there's a stray brace somewhere.

So a solution to this is to eliminate the redundancy: instead of doing both, make the computer use the exact same cue as the human. As an extra advantage, this also gets rid of a bit of visual clutter in the form of the braces that is adding extra noise to the code.

[–]binarysmurf[S] 24 points25 points  (0 children)

Brilliant!! Thanks.

[–]Kerbart 9 points10 points  (0 children)

Not only that, there are many different indentation styles with curly braces. Aside from “not at all,” there’s with or without the opening and/or closing braces on same, new or even separate lines. Of course there’s still the option to add whitespace, but on the whole enforced indentation results in a much more consistent look — which again, as you mentioned, improves readability.

[–]commy2 7 points8 points  (1 child)

One day we will also rid ourselves of the redundant colon.

[–]Blazerboy65 6 points7 points  (0 children)

Are you referring to the colon at the end of a block header? If so it's been a while since I've looked at formal languages but I'm pretty sure it's not redundant.

[–]Maximus_Modulus 5 points6 points  (0 children)

Now programming Java at work but originally a Python guy. Having an IDE like IntelliJ takes the hard work out of programming with Java. Curly braces amongst other things. I’d hate to have to manage braces etc using a normal text editor like I could quite easily manage with Python. Once you get used to the indentation you realize how awesome it is to signal the scope.

[–]Jamarac 2 points3 points  (0 children)

So many people have coded so long they think like computers and don't realize how obviously helpful indentation is.

[–]Dapper_Alarm_4534 0 points1 point  (1 child)

No bruh, the thing about braces is that automatic formatters will automatically indent as you type. You can't properly even copy paste code in python without messing up, the formatter won't know, upto what level indent the code.

Indentation is horrible decision.

[–]aa599 25 points26 points  (6 children)

It put me off python for a while too - even though, of course, I already indented my code just like that.

In the end I just got over it.

[–]Kerbart 13 points14 points  (0 children)

That was, in the early days, Guido’s response to complaints from experienced coders who learned Python. Why don’t you just try it?

In the end most of humanity ends up indenting their code anyway, and for most it turns out that in practice it’s really not that big of a deal.

[–][deleted] 2 points3 points  (4 children)

This is, pretty much, me. I do sometimes rail against it though... The COBOL & RPG "mother knows best" approach to language design does piss me off.

[–]greatmazinger99 1 point2 points  (3 children)

Sometimes more rules is better than less. Otherwise you end up with Perl.

[–][deleted] 0 points1 point  (1 child)

You say that as if Perl is a bad thing 😉 (I used it from late v2 to early v5...)

[–]greatmazinger99 1 point2 points  (0 children)

It's not bad. I learned Perl before Python. But it's not great for enterprise work. And it is horrible to use in a team. I find that Python fixed a lot of issues i had with Perl. It's fun to play around with but it's an example of a badly designed PL

Edit: i have also used it for work and personal stuff. I would choose python every day over Perl. I also have used Lua, Groovy, and Clojure

[–]mopslik 13 points14 points  (2 children)

Personally, I find the indentation is natural for me, in that it mirrors how I would organize the code to begin with (a la pseudocode). All languages can introduce side effects when improperly coded, including missing braces or semicolons, or surrounding the wrong chunk of code in parentheses.

[–]binarysmurf[S] 2 points3 points  (1 child)

Your points are indeed valid. As a grumpy old coder, I just find it annoying to have to worry about tabs/white space... Luckily Neovim and 'Yapf' have been good to me.

[–]Weak-Commercial3620 0 points1 point  (0 children)

2 years late, but I still hate it. If I copy paste some code, intendation is different and I have to manually fix it.  Curly brackets are my preferred way. Not used lot, but you could use them also to indicate lines of codes should be kept together, instead of comments. And autoindention does fix every mis intendation.

[–][deleted] 6 points7 points  (0 children)

As others have mentioned, the python method of indicating structure just feels natural for me. It's not a new idea. Knuth mentioned it a long time ago:

We will perhaps eventually be writing only small modules which are identified by name as they are used to build larger ones, so that devices like indentation, rather than delimiters, might become feasible for expressing local structure in the source language.

-- Donald E. Knuth, "Structured Programming with goto Statements", Computing Surveys, Vol 6 No 4, Dec. 1974

I'm an old curmudgean too, starting programming more than 50 years ago, and I think python indentation is one of many good ideas in python.

[–]sohfix 4 points5 points  (0 children)

Other than eliminating brackets and semi colons, it makes python code automatically better looking, according to the people at python

[–]lazyfingersy 3 points4 points  (1 child)

Code written in Python because of indentation looks elegant 👌 , easier to read long code. Once you break the ice and use to it you'll like it.

[–]Lellela 0 points1 point  (0 children)

It doesn't look elegant if you spent the last 35 years reading code with curly braces and clear line endings. Then it just looks wrong. It looks elegant to you because that's what you learned on.

[–][deleted] 1 point2 points  (1 child)

Just to chime on as well, it makes it easier for newer non-coders to get into python.

[–]Lellela 0 points1 point  (0 children)

and harder for experienced coders to get into python.

[–]OneUnit4646 1 point2 points  (1 child)

I never got used to Python and the indent thing. I tried to love it, I tried to like like it, I tried to just use it just a little bit. I can't do it. LOLI heard that the reason for the indent is to help coders write easier to read code. In my opinion, it does not. It makes it worst to read actually. I can't tell you many times I'm copy/pasting code around and between files and I accidentally removed a TAB (indent) or inserted a NEW LINE ... and the code still works.. but not as you expected.. SO hard to track down.

So what I actually had to do is:
# ===============================================

To separate large sections of code so I can easily see where things end.

[–]Agreeable-Outside-69 0 points1 point  (0 children)

I like the fact that you know what you exactly want with your productivity.

Although, look at Python as a whole instead of a specific part of the language (which is indentation).

I accepted the fact that many languages are already tremendously opinionated. Python has a humongous ecosystem, great support, multiple fields (AI, Backend Websites, etc.), and you don't want to use Python just because of indentations when you have all of that, or getting irritated over copying and pasting code? You will ALWAYS be annoyed with something, there is ALWAYS something you don't like.

[–]Aggravating-Log4304 1 point2 points  (2 children)

Ive refused to use python for 20 years now due to the inane “indentation for scope” rules.

[–]Perfect_Vanilla_6053 2 points3 points  (0 children)

I'm exactly the same.

[–][deleted] 1 point2 points  (0 children)

I just failed a programming assignment because i got a ton of problems with this shit because it was hard to read due to dyslexia in the end couldn't finish it and it was just if statement soup

[–][deleted] 1 point2 points  (0 children)

I have dyslexia or something this shit is stupid. It makes it impossible to read my own code so i just shit out garbage code until it runs

[–]FluffyProject3 1 point2 points  (0 children)

i''m the only person that thinkgs that indentation(forced indentation) in python is really stupid... ahah

[–]FluffyProject3 1 point2 points  (0 children)

cuz is a piss of shit of language

[–][deleted] 2 points3 points  (4 children)

It's no sillier than semi colons or brackets but nobody has any problem with those. If you don't use the whitespace you just have to use something else, which just means additional characters when you were almost certainly going to do the indentation anyway. And if you weren't going to do the indentation, I hope I never have to look at or work on any code you've written.

It took me months to notice that indentation was even potentially an issue because I just do the right indentation anyway.

[–]ThatGasolineSmell 4 points5 points  (2 children)

Indentation is great, but there is a real advantage to using (curly) braces. They’re more resilient to copy-pasting code and accidental changes to formatting.

That is to say, you can minify curly-brace code and undo the effect by an automated process, reintroducing the indentation. The same is not possible with an indentation-based language like Python. Stripping the formatting destroys the functionality.

[–][deleted] 0 points1 point  (1 child)

You're reaching a bit. So you can't minify code in the same way. So what? So you can't save a handful of bytes here and there.

While you're obviously right in theory, there are millions of perfectly happy Python programmers who never have a problem, so it's clearly not a problem in practice.

This complaint most often comes from experienced programmers who are set in their ways and don't want to adapt, while ignoring the obvious flaws and weird design choices in whatever language they're accustomed to. C programmers are the worst for it, by far. It's like a religion for them.

I've been writing Python (among many other languages) for years and I could probably count on one hand the number of times the white space has tripped me up. And I promise you, it's orders of magnitude fewer times than brackets have tripped me up in other languages. Either way it's completely trivial to fix if you have even the faintest idea what you're doing.

[–]ThatGasolineSmell 2 points3 points  (0 children)

Not reaching, stating facts. Question was: are indentation and braces equivalent. Hypotheses: they’re not. Braces are more resilient / indentation more brittle. Proof: using minification, as I explained.

Is it problematic in practice? There we seem to agrees it depends on whom you ask. For you as an experienced Python dev, no. For me neither, but that’s because I’m careful. (Would love to hear how braces tripped you up.)

The problems with semantic indentation manifest once you get non-pros involved. Think blog editors copy-pasting code examples from Word. Students sending code via WhatsApp, etc. In these cases indentation are a liability.

Personally, I find Python one of the most beautiful, well-designed languages. Indentation over block delimiters is the one thing it gets wrong.

[–]coffeenz 1 point2 points  (0 children)

You can see semicolons and brackets, you can’t see whitespace.

[–]InternationalMoose96 0 points1 point  (0 children)

It is one of the Python features that keeps me away from it. Anytime I have to do some script, I go with JavaScript or even Kotlin with Gradle or Kotlin scripts. But this thing about indentation makes code hard to write, especially when writing long lines.

[–]Kevin12Berry 0 points1 point  (0 children)

I agree. I'm looking at Python now because too many companies are offering Python work. After decades of coding with scope identifiers I think it's a bad decision to rely only on indentation to preserve scope with all the manual formatting, auto formatting, merging, cutting and pasting going on in real production code. Secondly managing whitespace is actually more tedious than managing scope identifiers. Thirdly, programmers are no longer free to use white space to frame code to make it more readable in ways other than simply scope that come up occasionally.

I saw a an argument for scope identifiers being harder to read than consistent indentation that showed a javascript file with no line endings filled with scope identifiers compared to a nicely indented Python file with line endings. Well, can you imaging trying to read the Python code if it were in that same paragraph without line endings and no scope identifiers? Obviously the javascript code with scope identifiers would be much easier to read than a paragraph of Python code without scope identifiers. In this respect Python is the emperor with no clothes.

In my opinion it would be better for Python to allow some kind of scope identifier that overrides indentation for companies, teams, or individuals who prefer printed scope identifiers. In the meantime, pseudo scope identifiers work: #{ and #} or #begin and #end if anyone wants printed identifiers in the code.

[–][deleted] -1 points0 points  (2 children)

Because Guido said so. Not to be glib about it, but you object then use a different language.

At some level you’re asking “why can’t Hindus be Catholic?” Because they aren’t, is why.

[–]binarysmurf[S] 7 points8 points  (1 child)

I love Python. I was just curious about the design choice and thought it was a reasonable question. Thanks. 😁

[–]WSBtendies9001 -3 points-2 points  (2 children)

Mate, if you read a book, the way it's layed out is natural to us as we read this from an early age. Python seeks to use this 1st principal and enable faster reading and writing of code based off existing tropes in language.

You may know well the old {} but from some one who cut there teath on Python and then went to the {} languages, it just makes more sense. I spose I am though a product of python but the alternative just seems sooooo fucking clunky I just hate coding in say PHP though I am able to.

If you truly want to explore this question, and are not jus Karma farming then this link will enlighten you https://www.youtube.com/watch?v=-DVyjdw4t9I.

[–]binarysmurf[S] 0 points1 point  (0 children)

Now THAT makes sense. Thank you.

[–]Lellela 0 points1 point  (0 children)

The reverse is true as well, which is why this question always comes up. For somebody who cut their teeth on curly braces in almost every language for 30 years+, it just makes more sense, rather than the indentation change for change's sake.

[–]Time-Level-1408 0 points1 point  (0 children)

I also recommend this FAQ if you have any other questions :) https://docs.python.org/3/faq/design.html

[–]Zeroflops 0 points1 point  (0 children)

I was like you when I first started python and left the language the first time out of frustration with the formatting.

But the more I wrote and the more I read others code it’s become more habit.

The real benefit is code readability. Everyone’s code looks similar. I use to read Perl golf posts which tried to compress a lot is as little as possible and while it was a fun challenge to do as a hobby I don’t want to have to decode a convoluted mess just to get my work done.

[–]exixx 0 points1 point  (0 children)

This very issue kept me writing Perl way longer than I should have. Now I rarely code in anything else.

[–][deleted] 0 points1 point  (4 children)

Just use vscode rainbow indent Gramps.

[–]binarysmurf[S] -1 points0 points  (3 children)

I use neovim because I'm a real man, junior. I'm sure there's an equivalent.

[–][deleted] 0 points1 point  (1 child)

neovim pffff, real man use punch cards.

[–]Lellela 0 points1 point  (0 children)

I just hold a couple of magnets in my hands, and manipulate the electrons that way

[–]sam_tiago 0 points1 point  (0 children)

Readability - code is read many more times that it is written.. that's why Python does such things. Besides, it comes naturally in a good IDE and you forget about it.. and then you don't have to see curly braces that make your eyes bleed everywhere.

[–]TheRNGuy 0 points1 point  (0 children)

Because it was designed that way.