Python bashing on Geekmuse

by Jeremy Jones

Related link: http://geekmuse.net/blog/comments.php?y=05&m=10&entry=entry051017-133741



I'm running a little behind on my podcast listening lately. I'm trying different podcasts to see which ones I want to fill my 2 hours of daily commute with. So far, my regulars are, O'Reilly's Distributing the Future, This Week in Tech, GeekMuse, and some of IT Conversations.

The second topic on Episode 11 of GeekMuse was about TurboGears, apparently a discussion of the recent Slashdot article which compared TG with Ruby on Rails. The GeekMuse discussion quickly turned away from TurboGears and toward Python itself. Some of the GeekMusers made comments of disgust about the semantic value of white space in Python. There was the obligatory comparison to Cobol. There was a disdainful mention of forcing users to adopt a consistent coding style. And there was also a question of flexibility, noting that sometimes it is more convenient or readable to just have an if statement on one line (which, by the way, Python supports).

All of these arguments are pretty entertaining to me. The discussion never moved past the whitespace issue. And from their discussion, this issue alone would keep the GeekMusers from ever adopting Python as their respective language of choice. No problem. It's a big world. There are plenty of good languages to choose from. Diversity makes the world go 'round.

It's just funny to me because the whitespace issue nearly kept me away from Python. And it's typically the first issue non-Python types bring up to point to why they wouldn't adopt Python. When I first looked into Python, I had been working with Perl and had tinkered a bit with C, so I was accustomed to curly brackets identifying beginnings and ends of code blocks. The whitespace in Python turned me off. I could never give the rest of the language enough of a glance to properly appreciate it. I don't remember what happened, but whitespace became less of an issue for me. I dug into Python and quickly became attached to the simplicity of the syntax and how naturally thinking about problems in this new language became.

Now, I enjoy the whitespace. It reminds me that I should be indenting code blocks anyway for readability. Yes, there are problems if one developer uses tabs (bad programmer!) and another uses four spaces like he's supposed to. But it makes the code so much easier to read and understand and troubleshoot problems.

So, how much of an issue should significant whitespace be? I personally think that every development organization and every development project (open source or not) should establish coding conventions which must be adhered to. Some of those conventions should entail the use of whitespace, even for Python where whitespace is meaningful. And it is my preference for whitespace to follow code blocks for readability. Maybe I'm wrong, but I think most developers would agree with me on this point. And if they do, what's the big deal? If everyone should be indenting properly, anyway, what's the problem with including whitespace as part of the syntax of the language? But, like I said earlier, it's a big world. There are plenty of good languages to choose from, most of which don't regulate where or how whitespace is scattered through source files. If mandatory whitespace gives you the coding heebie-jeebies, you can find a language other than Python which will suit you well.

10 Comments

mattfein
2005-10-23 13:26:52
Whitespace
I'm a big fan of Python and use it quite a bit, but I find whitespace errors are a continual (although minor) irritant. I suspect it's pretty common-- every now and then (often after a cut-and-paste) I get a mysterious syntax error. And then-- Ah, shit. Whitespace. Again.
jmjones
2005-10-23 14:16:18
Whitespace
I've honestly had very few problems with whitespace in Python. To be fair, though, one of the most troublesome experiences in my Python life has been whitespace related. There was a time I was trying to get someone's code integrated into my own and he not only used a different whitespace convention from me, he used a varying whitespace convention within his own code. It was mostly 3 spaces for an indent, but there were some places where it was 2 and others where it was 4. This was a huge pain to get working. I suspect, though, that you could wind up with just as many parentheses mismatches from copying and pasting Perl/C/C++/C#/Java as you could whitespace problems from Python. Maybe I'm wrong, but that's my suspicion.
jdonnell
2005-10-23 19:56:02
Whitespace
I don't understand how that was a problem. Let's assume you were coping a chunk of his code into yours and he had an if block within that code that used 3 spaces of indentation. This wouldn't cause any problems. I can't think of any circumstance where you would have a problem unless you were copying fragments of blocks and pasting them in to fragments of blocks, but I can't imagine ever doing such a thing.
simon_hibbs
2005-10-24 01:53:41
braces are just as much of a problem
One thing I love about Python is never again having to count the braces (or BEGIN/ENDs) and make sure they all match up. This has always been a problem in other languages. Logical structure is reasonably well represented by indentingin C or C#, so I arrely had problems getting that right, but when you're modifying code it's so easy to get the braces munged up.


In Python you just don't have that problem. You can sue the indenting to cue you in to the logical structure, and counting braces is a thing of the past. The elegeant simplicity of it all is realy refreshing. Why use two syntactical conventions for nesting structure, when you only realy need one?


Simon


P.S. Yes I know most programmers editors have brace-counting and highlighting features, but this is a crude workaround for a problem Python proves you just don't have to put up with.

simon_hibbs
2005-10-24 02:01:28
Whitespace
Code can be badly formatted and needlessly obfuscated in any language. It's true that Python's use of whitespace does open it up to abuse in a way that other langauges aren't, but even in C or Perl badly formatted whitespace can be very annoying - just not quite as much as it can be in Python.


In the end bad formatting of this kind is the fault of the programmer, not the language. Just because a langauge gives us enough rope to play with, that's no reason to blame it if we end up tying ourselves in knots that are easily avoidable.

jmjones
2005-10-24 04:59:22
Whitespace
Perhaps the problem was that I was attempting to apply my own coding standard to his code. I wasn't bringing in small chunks of code - I was bringing in a large block of code. I started noticing that some of the indentation didn't look right, so I started trying to get it all set to 4 spaces, which is my convention. It felt like the more code I modified to proper indentation, the more I had to change. Getting this code indented properly quickly became a tar baby.


So, to your point, I believe that the code initially worked before I started making it indentation-consistent. But if I'm going to work on some code and maintain it, I'd prefer to make sure it's indented properly.

aristotle
2005-10-24 08:14:09
Re:
Significant whitespace robs you of the power of sloppiness. When structure is carried in non-space characters, the computer can use those to figure out the proper whitespace for me.


I often ignore indentation when I’m rearranging bits of code with cut, copy and paste. Then in Vim I hit =ip and it’s all magically laid out nicely.


That’s simply impossible with Pyton.

happygiraffe
2005-10-24 09:39:09
not whitespace errors
People always talk about the problems with whitespace in Python. But it doesn't complain about whitespaces. It complains about inconsistency. Which as we all know is the bane of coders everywhere. Go python!


-Dom

nnes
2005-10-25 07:31:06
Go and write clutter in something else
Count braces or add whitespaces, they are equally painfull. At least in Python after the code has been written it is easier to read because it is free from braces/begin-end clutter.
john_betelgeuse
2005-10-29 08:31:33
Tabs vs. Spaces
All the above comments about problems with white space in Python prove just one thing: the author is wrong. All Python programmers should use tabs and only tabs for indentation.


One tab, one indent. Three benefits arise: 1) code can be cut and pasted between programs, 2) the individual programmer can set his display preferences any way he wants (to display a tab as three, four, eight, whatever spaces) and 3) the indentation throughout his program will always be regular.


My team does this, and we have no white space problems at all, at all. :-)