Mr Ed of Hacknot on Wikiphilia
Next

Python whitespace FAQ, or, Python is not Fortran 77

It's really rather shame that I should write a page like this in these modern, wise times, but I have heard people mentioning this stuff now and then.

So, does Python's significant whitespace suck because Fortran sucks?

No, it doesn't.

Should I refer to Fortran (77)'s whitespace syntax and semantics when discussing Python's significant whitespace?

No, you shouldn't.

Why not?

See Fortran 77 language reference. Python's significant whitespace just means that you need to indent your code correctly, which you should do anyway, and that's it. There are no fixed fields or anything like that in Python. See this old Linux Journal article by Eric Raymond for overcoming similar preconceptions.

But am I allowed to dislike Python's significant whitespace anyway?

Sure, as long as you don't refer to Fortran (77).

Should I dislike Python's significant whitespace?

No, you shouldn't.

Why not?

Because it's the most natural thing, and, while it's a lovable feature of Python, it's not that big a deal anyhow. It shouldn't be a show-stopper.

But it's ugly and wrong.

No, it's not. It's beautiful and right. But this isn't going anywhere with us arguing whether it's right or wrong. The point is that if the only reason holding you back from trying out Python is its significant whitespace, then you're really stupid. I mean a total idiot. Even if the significant whitespace is only the proverbial straw that broke the camel's back, you're still an idiot for not trying out Python because of this.

You need not like it after you've really tried it. You may as well say that you don't like the significant whitespace because you couldn't get comfortable with it. I would find that odd, but I wouldn't argue with you on that. However, if you dismiss Python as a viable alternative because of its significant whitespace; well, as I said, that's just stupid.

But it's so brittle!

No, it's not.

I know of zero cases where a non-newbie has unintentionally screwed up his Python program totally because of the significance of the whitespace. That just does not happen. Again, these are not issues that make programming hard or easy (and, believe me, there are things that really make programming hard), but are of rather minor importance.

I am not saying that messing up one's Python program is something totally unheard of, but, on the other hand, I personally know of many cases where screwed-up indentation in a language that doesn't have significant whitespace has lead a programmer to astray. Yeah, sure, from time to time you introduce a bug to your program by mis-indenting in Python, but this happens in other languages too (only in reverse, because of the insignificance of the whitespace). On my personal account, then, significant whitespace has saved me from more trouble than it has put me into.

Well, the indentation might get screwed in web pages or emails, right?

Yeah, sure. And that's an annoyance. But if the code is anything more than ten lines, it should be laid out by a professional anyhow and if the code is something significant, you shouldn't distribute it that way. If the code is an example, it's a nuisance if the indentation gets screwed, but you can work the indentation out yourself, too.

(My own code examples in the archives of this blog are actually broken, because of some change in the blogging software I use. You can more easily copy & paste the Java code in my archives, right now, but I really should just fix the mark-up.)

If you're distributing the code in regular script files, the significant whitespace poses no extra threats.

It's difficult to program Python in Notepad/Word/Some-other-exotic-editor because of the significant whitespace.

Yes, it's a bit difficult, though not impossible. I hope you don't get too upset by this, but I'll have to be insulting you again: if you program in Notepad, then you are stupid. You are, believe me.

If you don't know what editor you should use, see the page Python Editors.

It's difficult to move around big code blocks because of the indentation issue.

This is true and a real issue. There are features in advanced editors that make it easier, but it's not real easy with them either. But it's not like you can move around big blocks of code easily in every other language either. The program might be syntactically correct after moving the code block, but you still have to fix the indentation.

But this is really also a question of one's personal programming habits.

(words: 758)

[9 comments]

Filed under:

Comments:

Posted by Jarno Virtanen at 20.02.2005, 07:24

shift-region-left/right certainly helps, but it's pretty sensitive stuff anyhow. I have not made any measurements, but it definitely feels a bit slower (for me) to move around code blocks in Python vs. in Java.

Posted by Jarno Virtanen at 23.02.2005, 07:14

It was not actually my weblogging software that broke them, but my own tweaks on top of it. I just wanted to say this so that I get blaimed for breaking stuff, not others. :-)

But yes, it's still a good example how significant whitespace is sometimes a inconvenience.