C++ is my first language, and as such I'm used to whitespace being ignored. However, I've been toying around with Python, and I don't find it too hard to get used to the whitespace rules. It seems, however, that a lot of programmers on the Internet can't get past the whitespace rules. From what I've seen, peoples' C++ programs tend to be formatted very consistently with respect to whitespace (or else it's pretty hard to read), so why do some people have such a problem with whitespace-based languages like Python?
-
16You call Python a whitespace-based language? This is a whitespace language! http://compsoc.dur.ac.uk/whitespace/ – David Apr 22 '10 at 23:47
-
@David - you beat me in posting about Whitespace by 60 seconds. – Bert F Apr 22 '10 at 23:48
-
there are lots of Perl programmers on the Internet :-) – Anycorn Apr 22 '10 at 23:50
-
6Fewer and fewer every day........ – Nathan Osman Apr 22 '10 at 23:51
-
3I don't see this as subjective and argumentative. It's not saying "Python sucks", which is S&A. It's asking why some people hate it, which clearly they do. In essence, it's asking what's their beef with Python. So voting to reopen. – cletus Apr 23 '10 at 03:47
-
2@cletus: "In essence, it's asking what's their beef with Python." So then the *answers* end up being S&A instead of the question, which isn't much better. – gnovice Apr 23 '10 at 05:23
-
@gnvoice that's not a sufficient reason (imho). In fact, are any other of the answers here argumentative? Basically people are too trigger happy in S&A closures. – cletus Apr 23 '10 at 05:27
-
1@cletus: I'm with gnovice on this. This is a hard questions to answer in a neutral tone. To the point that *your* answer---reasonably thoughtful though it is---comes down to "Because they're dumb." in slightly politer words. S&A all the way. BTW-- I suspect in many cases the answer is that they had a bad experience with make in their childhood. That kind of thing can really leave its mark. Me, I mostly like python's style. – dmckee --- ex-moderator kitten Apr 24 '10 at 20:59
-
1@druckee look at the history. Reopened by moderator. Besides, "because they're dumb" is not an accurate characterization of my post. Giving up control isn't an issue of intelligence, it's mostly a personality thing. – cletus Apr 24 '10 at 23:08
-
1Code generation is the biggest reason why people find whitespace sensitivity inconvenient – Yuhta Nov 16 '14 at 00:28
-
Not formatting styles, readability, and such? – foo Aug 09 '16 at 10:01
-
Hmm, closed then reopened then closed again. Frustrating. I was gonna give what I think is a non-nonconstructive answer, listing some ways in which python's handling of whitespace puts real (and frustrating for me) limits on the things you can do with python code. – Don Hatch Nov 08 '19 at 03:30
-
I do think this is an important question. I wonder, is it *possible* to ask it in such a way that it doesn't get closed as S&A? I was looking for other similar questions, and found https://softwareengineering.stackexchange.com/questions/16141/what-are-the-pros-and-cons-of-a-language-using-whitespace-vs-s-to-indicate-s#question-header which got my hopes up. But then I found that's closed too :-( – Don Hatch Nov 08 '19 at 04:06
11 Answers
It violates the Principle of Least Astonishment, because we have it ingrained in ourselves (whether for good or bad) that whitespace Does Not Matter in a programming language. Whitespace is one of those issues that has been left up to personal style.
I still have bad memories back from being a student of learning the hard way that 8 spaces is not equivalent to a tab in a Makefile... Ah, the sleep I lost...

- 53,118
- 13
- 86
- 159
-
4As this is perhaps the most fundamental Python rule, I find it very hard to believe anyone could be surprised by Python's whitespace choice in the midst of coding. In fact, Python **by far** follows the principle of least astonishment more than any other language in existence, bar none. – BlueRaja - Danny Pflughoeft Apr 23 '10 at 00:04
-
22"In fact, Python by far follows the principle of least astonishment more than any other language in existence, bar none." What you've got there, sir, is a controversial opinion stated as fact. It really depends on how you came into programming. – temp2290 Apr 26 '10 at 17:39
-
This is a *horrible* answer. It teaches an incorrect and dangerous understanding of the principle of least astonishment, which is about being consistent *within the same language* so code doesn't act in an unexpected way, not avoiding things you aren't used to and don't want to adjust to. Otherwise we'd all still be programming in C, Delphi, Cobol, and VB. Improvements are made to programming languages and we should embrace them (up to you to decide if this is one). Python is also one of the most taught languages to students, so pretty soon anything other than indention will be "unexpected". – dallin Dec 07 '22 at 01:44
The only valid reason I have come across is that refactoring using cut-and-paste (not copy) without refactoring tools (or syntax-aware cut-andpaste), can end up changing semantics if an easy mistake is made.

- 145,806
- 30
- 211
- 305
People hate it because it violates common sense. Not a single one of the replies I have read here decided that it was ok to simply forgo periods and other punctuations. In fact the grammar has been very good. If the nonsense about indentation actually carrying the meaning were true we would all just forget about using punctuations entirely.
No one learned that newlines terminate a sentence in a horizontal language like English, instead we learned to infer when a sentence ended regardless of whether or not the punctuation was present or not.
The same is true for programming languages, especially for those of us who started out with a programming language that did use explicit block termination. You learn to infer where a block starts and stops over time, it does not mean that the spacing did that for you, the semantics of the language itself did.
Most literate people would have no problem understanding posts without punctuations. Having to rely on what is a representation of the absence of a character is not a good idea. Do any of you count from zero when you make your to-do list?

- 111
- 1
- 2
-
2I'm trying really hard to understand the argument here, but I'm having trouble connecting the dots :-( You seem to be saying a lot of "If A were true then B which is absurd" where I don't actually see the connection between A and B. – Don Hatch Nov 08 '19 at 02:03
-
The analogy is European orthography here. But when we talk about mathematics, it's kind of common sense to use newlines or different locations on a sheet of paper to keep things apart. In orthography paragraphs are used to separate ideas. The analogy between orthography and programming languages does not work. I have yet to meet someone who seriously thinks, code needs to be written like orthographic text in a single line. The design and purpose of the style is very different between orthography and programming. – ChrisoLosoph Oct 27 '22 at 18:15
Alright, this is a very narrow perspective, but I haven't seen it mentioned elsewhere: keeping track of white space is a hassle if you are trying to autogenerate a script.
When I first encountered Python, I don't remember the details, but I had developed a Windows tool with a GUI that allowed novice users to configure several settings, and then press OK. The output of the tool was a script, which the user could copy to a Unix machine, and then execute it there to do something or other that was too complicated or tedious for them to do manually. Since nobody maintained the generated scripts, there was no reason they needed to look nice. So, keeping track of indentation seemed like an unnecessary burden from that perspective.
For most purposes, though, I find that Python is much easier than any other language.

- 3,840
- 19
- 20
There are several different types of whitespace (spaces, tabs, weird unicode characters, carriage returns, line breaks, etc.), they aren't necessarily visually distinct, and languages and editors may treat them capriciously. This isn't an argument against well-designed whitespace semantics, but many people are against all forms of it simply because of the possibility of poor design.

- 4,050
- 1
- 25
- 33
-
You can just show them in your editor. Like ALT+SHIFT+P show whitespace, M-x whitespace-mode. Use M-x tabify or whatever in your editor to turn spaces to tabs. – aoeu256 Sep 02 '19 at 03:01
-
I'm a bit confused. First you point out "There are several different types of whitespace (spaces, tabs, weird unicode characters, carriage returns, line breaks, etc.), they aren't necessarily visually distinct, and languages and editors may treat them capriciously.". I think that does a good job of describing why it's impossible to have well-designed whitespace semantics. But then you say "... but many people are against all forms of it simply because of the possibility of poor design." This implies good design is possible. What would good design look like to you? – Don Hatch Nov 08 '19 at 02:10
Perhaps your C++ background (and thus who your peers are) is clouding your perception of this (ie selective sampling) but in my experience the reaction to Python's "white space is intent" meme is anywhere from ambivalent to they absolutely love it. The reason a lot of people love it is that it forces people to format their code.
I can't say I've ever met anyone who "hates" it because hating it is much like hating the idea of well-formatted code.
Edit: let me put this in some perspective.
In the Java world there are two main methods of packaging and deploying Web apps: Ant and Maven.
Ant is basically an XML-based Make facility that has tasks for the common things you do. It's a blank slate, which is powerful, but it also means you have to write a lot of common things yourself and every installation is free to do things slightly differently. All of this is well-intentioned but can make it hard to figure out someone's Ant scripts.
Maven is far more fully features. It has archetypes, which are basically project types. Depending on which archetype(s) you use, you won't have to write any tasks to start, stop, clean, build, etc but you will have a mandated directory structure, which is quite deep.
The advantage of that is if you've seen one Maven Web app you've seen them all. You know the commands. You know the structure. That's extremely useful.
But you have people who absolutely hate Maven and I think it comes down to this: they don't like giving up control, even when it's ultimately in their interest to do so. Also, you'll find a certain brand of person who thinks that their use case is a justifiable exception. You see this personality trait a lot. For example, I think an old Joel post mentioned a story where someone wanted to use "enter" to go from the username to password form fields even though the convention was that enter executed the default action (usually "OK") so they had to write a custom dialog class for Windows for this.
Basically some people just don't like being told what to do and others are completely obstinate in their belief that they're right even when all evidence points to the contrary.
This probably explains why some supposedly hate Python's white space: they don't like being told how to format their code. They like the freedom of C/C++.

- 616,129
- 168
- 910
- 942
-
5+1 I've met programmers who hate Python's formatting, but these are the same people who love C and assembly and have never done any serious coding in either. – BlueRaja - Danny Pflughoeft Apr 22 '10 at 23:57
-
3Indeed, forcing people to format their code is a good thing. Code formatting is very important as a project grows, nobody wants to track something down in a 10k LOC thingy when every other file has a different coding style, so Python makes beginners think more about this. Also since you're now using indentation for both blocks and style, you can get rid of those stupid curly braces which by the way(like most programming characters) are ways off on a German keyboard. And nobody can tell me that he's doing serious work without an Editor/IDE that doesn't feature auto indent/tabs to spaces conver. – Ivo Wetzel Apr 23 '10 at 00:21
-
"The reason a lot of people love it is that it forces people to format their code." This makes sense. I personally can't stand to see inconsistently-formatted C++ code, so having the language itself force it could be nice. – Maulrus Apr 23 '10 at 00:28
-
13We hate it because it breaks when white space changes. When Make and cron were written, that was wrong, okay. It's still wrong. It'll be wrong in the year 1e9. Somebody's going to screw it up reformatting code, and most code diff tools will, sensibly treat different kinds of white spaces as equivalent. – Tim Williscroft Apr 23 '10 at 05:06
-
5Think about a human language that changes its meaning depending on formatting. Can blow the world up. We hate this concept because that. – Renascienza Nov 13 '15 at 02:27
-
4Nothing is more fun than hunting a bug where the bug is that a space was used where a tab was required or vice versa. Bugs that are invisible in the code are awesome. – Neutrino Feb 28 '17 at 14:44
-
1If they wanted people to really be forced to format their code, then spaces around operators should have been also enforced. – Iharob Al Asimi Apr 29 '17 at 11:50
-
3*I can't say I've ever met anyone who "hates" it because hating it is much like hating the idea of well-formatted code.* By this logic, Python should also have enforced comments above function and class definitions, because duh, comments are really important. – Mayank Verma May 28 '18 at 21:24
-
2Languages like C provide with a tool called `indent` on most Linux distributions. I don't see a reason why people can't simply use a tool to make code readable and need a 'revolutionary' programming language to fix readability problems. – Mayank Verma May 28 '18 at 21:30
-
Comments can make code harder to read unless the comments tell you something that isn't in the code, and if you need comments to explain your code you should've just rewrote the code to make it understandable. – aoeu256 Sep 02 '19 at 02:55
Because change is scary. And maybe, among certain developers, there are some faint memories of languages with capricious rules about whitespacing that were hard to remember and arbitrary, meant more for compiler convenience than expressiveness.
Most likely, not giving whitespace-significance a fair shake before dismissing it is the real reason. Ask someone to fix a bug in a reasonably complex but well-written Python program, then ask them to go fix a bug in a 20 year old system in C, VB or Cobol and ask them which they prefer.
As for me, I have as much trouble with whitespace in Python or Boo as I have with parentheses in Lisp. Which is to say, none.

- 19,244
- 4
- 34
- 61
-
Your comparison to a 20 year old system in C, VB, or Cobol seems to be a mixture of a strawman and a non sequitur. First of all, VB and Cobol *are* whitespace sensitive, aren't they? So I'll ignore those. Also where did the "20 year old system" come from? That's unfairly handicapping your opponent. So I'll ignore that too. So, you're left comparing a well-written python program with a well-written C program. Even *if* you're right that the python program is more maintainable, mightn't it be that that's *in spite of* python's whitespace rules rather than because of them? – Don Hatch Nov 08 '19 at 02:33
-
VB is not whitespace-sensitive (other than sometimes having a minimum of one space between some tokens, I suppose) and isn't used for structural semantics (certainly there are conventions for readability). Cobol also only uses whitespace as a token separator, IIRC, but I could be wrong. The structure of code in Python is enforced by whitespace. Whereas in C it's only a convention, so you can "lie" with it. In python structure is directly tied to predictable behavior, which makes it readable _because_ of semantic whitespace. Maintainability is closely tied to readability. – JasonTrue Nov 08 '19 at 02:45
-
Anyway the point isn't the age of the other program, but the fact that you're going to have to make sense of the code before you can change it; the age was just a proxy for "some code you probably aren't that familiar with." It's easy to be well-written by C standards and still harder to read than Python. – JasonTrue Nov 08 '19 at 02:48
-
Regarding "20 year old system"... ok, then are you willing to make an edit that puts all the contenders on an equal footing? That is, all of them get to be "a 20 year old system" (or, equivalently, all "code you aren't that familiar with"), and all of the programs "reasonably complex but well-written". – Don Hatch Nov 08 '19 at 02:58
-
Regarding your reply 'The structure of code in Python is enforced by whitespace. Whereas in C it's only a convention, so you can "lie" with it. In python structure is directly tied to predictable behavior, which makes it readable because of semantic whitespace. Maintainability is closely tied to readability. ' Okay *now* you've made some reasonable arguments, with justification. I think your answer would be well served if you would replace some of the specious reasoning in the answer with the better reasoning you gave just now. – Don Hatch Nov 08 '19 at 03:06
-
Regarding VB: I don't know VB, but [this](https://stackoverflow.com/questions/1411711/using-colons-to-put-two-statements-on-the-same-line-in-visual-basic) seems to imply you can't just join two lines, implying it is whitespace-sensitive. Regarding Cobol: I don't know cobol, but you may be right: [this](https://stackoverflow.com/questions/8959489/why-does-cobol-have-to-be-indented#answer-8962494) seems to imply Cobol is at least *less* whitespace-sensitive than it used to be. Still, I'm not sure "a reasonably complex but well-written program" is possible in either language, for other reasons. – Don Hatch Nov 08 '19 at 03:38
-
Conversely, I'm not sure "a reasonably complex but well-written program" is possible in python, either, again for reasons having nothing to do with whitespace (more to do with python's awkwardness when trying to scale a system involving several layers of modules and scopes). Given that, I find it difficult to cast your suggested exercise (that is, comparing a "reasonably complex but well-written program" in the respective languages) as an apples-to-apples comparison that would have anything clear to say about the whitespace question. – Don Hatch Nov 08 '19 at 03:56
-
Boohoo, I hate syntax because I don't get it. I want to sacrifice everything a real programming language has to offer because I don't understand that semicolons are there to indicate the end of a statement. I want to say python has better formatting because I love to endlessly scroll horizontally. – Hawkeye4040 Oct 25 '21 at 12:43
Because they are used to languages like C and JavaScript where they can align items as they please.
When it comes to Python, you have to indent code based on its context:
def Print():
ManyArgumentFunction(LongParam1,LongParam2,LongParam3,LongParam4...
In C, you could do:
void Print()
{
ManyArgumentFunction(LongParam1,
LongParam2,
LongParam3,...
}

- 71,149
- 71
- 256
- 361
-
3that's actually not a valid example. For Python, inside of `(` and `)` you can have whatever white space you like. – Ross Rogers Apr 22 '10 at 23:51
-
4Yea, you can do the same thing you did in the C sample code in Python. You write each param on its own line. – jcao219 Apr 22 '10 at 23:54
-
1Heh-heh. I never realized that. Thanks for pointing that out. **But** my first point is still valid. – Nathan Osman Apr 22 '10 at 23:58
-
1Even in your example, `{` and `}` are redundant data that clutter readability. All the code hierarchy information is encoded in the spacing that you've done. Most people read code blocks based on the indentation anyway. Such that accidentally omitted `{` and `}` can be hard bugs to find. – Ross Rogers Apr 22 '10 at 23:59
-
Basically its circle jerk, people have been coding in 10 years with braces so they aren't willing to try anything new... – aoeu256 Sep 02 '19 at 02:58
They will have to get used to it. Initially I had a problem my self trying to read some examples but after using language for some time I started liking it.
I believe it is a habit that people has to overcome.

- 1,186
- 3
- 14
- 28
Some have developed habits (for example: deeply nested loops, unnecessarily large functions) that they perceive would be hard to support in a whitespace sensitive language. Some have developed an aesthetic dislike for hanging indents.

- 117
- 4
-
Yeah, in Python its much easier to have shorter functions with list comprehensions, tuples (for return values, and for assignment...), no }\n, iterators, Python's "JSON" etc... – aoeu256 Sep 02 '19 at 03:03
The only complaints I (also of C++ background) have heard about Python are from people who don't like using the "Replace Tabs with Space" option in their IDE.

- 3,261
- 3
- 30
- 37
-
2I don't even see what tabs-vs-spaces has to do with any of this. I favor tabs for indentation, spaces for alignment, but you can write clean Python with both indentation styles. – antred Apr 06 '16 at 15:34
-
@antred To answer your question of what tabs-vs-spaces has to do with this: when working in a whitespace-sensitive language like python, if a collaboration group doesn't have a "no tabs in submitted code" policy, it is pretty much guaranteed to be a comedy of errors when someone who likes their tabs to be 8 spaces edits a file created by someone who likes their tabs to be 4 spaces, for instance. Similar hilarity ensues when trying to copy-paste each other's code out of web pages and emails. – Don Hatch Nov 08 '19 at 02:20
-
@Don Hatch Yes, if you use spaces for indentation then everyone has to agree on how many spaces represent one level of indentation. If you use tabs for indentation then everyone has to agree to use tabs for indentation. Both styles require that the project members agree on a set of rules. – antred Nov 11 '19 at 14:18