Initial thoughts on Python


Categories: GeekStuff

Well, for one reason and another, I have to learn Python. Like many people, I started out disliking the whitespace sensitivity. I’ve already been pretty badly burned by it at least once – to be fair, that’s partially because my editor likes to use tabs.

… And that’s where we run into my fundamental gripe about Python. It’s smug. The designers and developers have decided that they have found The Right Way To Do Things. If your editor likes to convert spaces to tabs, you’ll get screwed pretty badly sometimes, but it’s your fault for having a “misconfigured” editor. Never mind that the editor still needs to be set that way for everything else you work on; it’s misconfigured because it hasn’t been changed to adapt to the Python Way.

There’s a lot of preening in stuff about Python about how “readable” it is. I’m not convinced. Yes, it has less punctuation than C. Does less punctuation make things more readable? If so, someone should let all the people who are writing books about grammar know that punctuation is a bad idea and they should change English away from using it, because punctuation is unreadable and therefore bad.

I don’t wanna come across as being overly hostile to this. I mean, heck, at least it’s not PHP. And getting away from the damning with faint praise territory, it really is a pretty simple, clean, language to work in thus far. I can see people liking that, and there are tasks for which I might like such a language.

However, all this stuff about the whitespace sensitivity and how wonderful it is and you’ll grow to love it comes across as Stockholm Syndrome. Wait until the first time you have a hunk of Python in which something unknown went wrong with the indentation, and then see how clear and legible it is. You are by definition given no cues as to where the blocks end. You aren’t even allowed to have cues as to where the blocks end unless you want to add tons of extra comment lines.

One of the reasons I’m writing this is that the book I was reading smugly told me that if I don’t like the whitespace thing, to come back six months later and see if I still dislike it. Okay, yeah, I get it, you’re older and smarter and more experienced and just plain better than us plebes who write in C and Ruby and other languages that are not designed about looking pretty to people who don’t know them.

(EDIT: A bit over two years later, I still hate it, and it still causes me trouble regularly.)

The language itself isn’t too bad, but the smugness of the developer community is pretty offputting. I get along better with C and Ruby programmers, who are quite willing to acknowledge that their favorite languages have quirks and flaws.

Comments [archived]

From: g
Date: 2010-09-19 15:30:35 -0500

Can your editor be made to treat tabs differently when editing different sorts of file?

As for the rest: a lot of this comes down to matters of taste, and it certainly isn’t for me to say what you will or won’t come to find bearable. Obviously every experienced Pythonista either never had a problem with significant indentation, or has come to find it OK — but that’s just because anyone who finds it persistently annoying is likely to stop using Python. (I’m in the “never had a problem” category; I’ve also never been bothered by the parentheses round the conditions for if/while in C, nor by all the parentheses in Lisp; I guess I’m just lucky.) I’ve also scarcely ever had (or had to deal with) indentation-mismatch-induced errors in C. But what I have had is the situation where developer A finds developer B’s (or project C’s) code unpleasant to work with because they have different preferences for the placement of braces and indents. That basically never happens with Python.

I’d be reluctant to judge a language by the things in it that cause pain while I’m learning it and (if appropriate) tweaking my tools to work well with it. The things that bothered me when I was learning C or Python or any other language are lost in the noise now; what matters much more is what the language is like to use once one’s reasonably well used to it. I find that Python does well for me on those terms; your mileage, of course, may vary.

Anyway: best of luck with Python. May you either grow to love the significant whitespace, or else find a way of not having to use it in the future :-).

From: Peter Seebach
Date: 2010-09-19 13:35:41 -0500

It may be a bit unfair. So far, I’ve had one problem from whitespace that took me fifteen minutes to fix. I wanted to change the indentation level of some code, but my editor had put in tabs (and I can’t disable that without breaking it for editing Makefiles, which I also have to work on!), and when I inserted four spaces in front of it, many but not all lines got indented. The problem is that, since I didn’t notice instantly, I ended up with code in which there was nothing to tell me what I intended except re-reading the code.

In C, if indentation doesn’t match the actual program flow, you have a big red flag to tell you something went wrong. In Python, indentation always matches program flow, meaning you don’t have a check bit any more.

I may well get to a point where I’m no longer quite as offended by the whitespace thing, but as of this writing, I do not think it is possible that I will ever think it is a good idea. None of the alleged benefits strike me as actually beneficial, with the possible exception of saving a line at the end of a block.

In over twenty years of using C, I’ve had problems caused by indentation not matching program flow all of maybe once or twice. Ever. If I ever have another whitespace problem in Python, Python’s decision will have caused me more trouble in that short period of time than C has in over twenty years.

As to the “developer community” — I mean a combination of, say, people writing books on the topic, and people posting in comp.lang.python. By “developer community”, I mean the community of developers using Python, not necessarily just the people who maintain the language.

You do have some point about the natural language and punctuation thing. It’s just that I get very sick of this constant assertion that punctuation is bad for readability. No, it’s not.

From: Peter Seebach
Date: 2010-09-19 18:58:50 -0500

My editor of preference (classic nvi, NOT vim) doesn’t have obvious hooks for changing tab behavior based on kind of file. Maybe it has a way, I just don’t know it.

I’ve definitely had issues with uncomfortable indentation styles while working with other peoples’ code, but usually I find that basically any style which is consistent is livable for me. I can see some appeal to a firm and consistent set of indenting rules. Honestly, I could probably live with the whitespace sensitivity if there were block enders, and the language simply enforced that it is a syntax error for the block ending things not to be correctly indented. I don’t care that much either way about the whitespace per se; I care about not having a backup cue to tell me what was happening if whitespace gets lost.

I’m not too worried, in the long run. I mean, heck, I have lived through having to use PHP, nothing else holds any terrors for me. :)

From: g
Date: 2010-09-20 04:24:44 -0500

If you find block enders useful as a cross-check on indentation, you can always put them in. Just use, say, comments consisting only of a “#” character. Unless either your editor or your cut-and-paste habits are very strange indeed, that ought to give you about as much protection as close-braces do in C.

Of course, that won’t help when you have to deal with other people’s code, and it may look a bit strange if other people have to deal with yours, but it might help your peace of mind if you’re worried about indentation screwage.

I’m not going to risk turning this into a hybrid language/editor flamewar, beyond saying that I bet you can make vim behave almost exactly like nvi except for treating tabs differently in different kinds of file. Whether it would make the slightest bit of sense to do that probably depends on (1) just how much time you have to spend working in Python and (2) whether, having done it, you’d find other vim features useful. (On #2 I’m guessing that you want to avoid growing dependent on vim features that aren’t in other vi-family editors, so that you can use any machine and have your favourite editor there; the tradeoff between that and having extra flexibility most(?) of the time is one I wouldn’t dream of trying to make for you.)

From: g
Date: 2010-09-19 03:37:42 -0500

I think you’re being unfair at a couple of points.

1. Punctuation and natural languages: (a) What makes for a good natural language is not necessarily the same as what makes for a good programming language. COBOL was not the greatest ever experiment in programming language design. (b) English has, in fact, substantially less punctuation than Python, so why should Python partisans want it to have less still?

2. I don’t interpret “if you don’t like this, come back after trying for 6 months and see if it’s still a problem” as meaning that the author is older and wiser and more experienced and better than you. I take it as meaning only that they’ve got more experience of how people respond to Python, and have found that many people (a) are put off by having significant whitespace but (b) after using Python for a while, find that it isn’t a problem for them after all.

For what it’s worth, I have been using Python for many years and I don’t think I’ve ever had a problem caused by whitespace sensitivity that’s taken more than 15 seconds to resolve. And I can’t remember the last time I had any such problem at all.

3. You say “the smugness of the developer community”, but everything you’ve said before that about smugness seems to be about the language itself and a couple of things written about it. Have you actually been interacting with Python developers and finding them smug? (My own experience on that score: some of them are, some not, just as with any other programming language with the possible exceptions of deliberately crazy ones like Brainf*ck or Intercal.)