Python whitespace FAQ, or, Python is not Fortran 77
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)
[permalink] [11 comments] 19.02.2005, 08:19
- Comments:
Posted by Bob Ippolito at 19.02.2005, 11:55
It's definitely possible for a non-newbie to screw up indentation if they're on somebody else's machine and the editor they're using doesn't expand tabs into spaces.. of course, a non-newbie realizes this right away and knows how to fix it, but it's not at all obvious visually or by the error message.Moving around big code blocks is easy in just about any editor you should be using.. every editor I've ever used for more than a few minutes can do it with a keystroke or two. People need this functionality in other languages too, whether or not the whitespace is significant.
Posted by Karl Ramm at 19.02.2005, 16:41
Turning the question around: even in those "other" languages, you still need to get the indentation right when you move large code blocks around if you're doing it "right". Python just "makes" you do it "right".
Posted by Mark Eichin at 19.02.2005, 23:14
The difference is that in other languages, you move the block, then "reindent whole function" and the right thing happens. A proper python editor needs to be smarter than that (and emacs isn't, for example.) "yank-at-correct-depth" might help (mostly when I do this I use a rectangle-cut to correct the extra whitespace - after all, block moving mostly happens as a part of refactoring and "take this useful block of code into a function of it's own" which generally involves dropping an indentation level.Hmm, a function that assumes the indentation *profile* is correct and just normalizes the amounts would actually work nicely for that case...Also, about the posting issue (which turns out to be similar to the spoken-text-code-reading issue) - there's some tool in the distro that uses #end if
and the like as markers for outdents, which doesn't seem to have caught on much but is sufficient to not lose the information.Posted by Alex at 20.02.2005, 01:57
I find cutting and pasting python code to be pretty easy in emacs, actually. Just cut, paste, then set to the correct indentation with the python-mode bindings C-c C-l and C-c C-r (py-shift-region-{left,right}).
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 Jim at 23.02.2005, 05:53
It's a pain in the neck when you deal with other things that need indenting. Such as large SQL queries, or HTML when you're using things like mod_python.The curly braces, semi-colons and insignificant whitespace approach is much more convenient, and this is coming from somebody who maintains consistent indentation all the time.I don't dislike significant whitespace because I'm lazy, I dislike it because it gets in the way. The fact that your weblogging software broke your Python examples is just another example of this inconvenience.
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.
Posted by Chris Lees at 27.02.2005, 06:31
My first programming/authoring language was Hypercard (HyperTalk), which also used significant whitespace and an English-like language. I wrote programs in that language when my age was in single-digits.It isn't rocket science to use valid indentation, especially when using an editor that automatically adds the correct whitespace when starting a code block. As all Python programmers say, you SHOULD use correct indentation when working with any language. How can it get in the way if you're going to use it anyway?If you miss having an ending character for a code block, observe what I sometimes do:if n:
pass
# end ifPosted by flash at 06.04.2005, 05:47
i hate indent
when large block, it could be very awful...usually i will put remark and my "extra newline"
to make the code easier to readit there any possible to add a setting that:
i want to use { } for my block ??example in other language:// 1.0
if( ... ) { // part 1, do something
blah blah
blah blah blah
...
... // part 2, do something
...
...
// 2.1
if( ... ) { /* remark, describe about
why it comes to here
...
*/ ....
.... } // end of 2.1} // end of 1.0Posted by flash at 06.04.2005, 05:48
er... the "tab" was not show, i re-post it with space:// 1.0
if( ... ) { // part 1, do something
blah blah
blah blah blah
...
... // part 2, do something
...
...
// 2.1
if( ... ) { /* remark, describe about
why it comes to here
...
*/ ....
.... } // end of 2.1} // end of 1.0Posted by flash at 06.04.2005, 06:25
oops, the "" didn't show correctly.re-post:// 1.0
if( ... ) { // part 1, do something
blah blah
blah blah blah
...
... // part 2, do something
...
...
// 2.1
if( ... ) { /* remark, describe about
why it comes to here
...
*/ ....
.... } // end of 2.1} // end of 1.0