Simply Jonathan

Python indenting

I don’t mind Python’s indenting. Sure, it took a little time to get used to it, coming from PHP with all its braces. But it wasn’t really hard.

The only problem I see with its indenting, is the fact that it’s variable. You can use a single space, two spaces, three spaces, […] Heck, you can use 32 spaces or even tabs. Flexibility is often good, but this introduces a problem, which has been touched upon a lot by others as well: Different programmers end up with different tab indexes, making their coding conventions non-interoperable.

To make matters worse, Python treats a tab as an 8 space indent. So the following could happen: one programmer uses tabs as indents, has them shown as 4 spaces, and hacks away happily. Then someone else takes a look at the code in their editor. He might be using an 8 space indent, and so there should be no problems. He edits the code a bit, then sends it back to number one. He then gets mighty confused — why is there all of a sudden a double indent there?

You see how this can scale and get out of control, don’t you?

So what do I propose, in order to fix this? I see two solutions, the one preferable, the other one realistic:

  1. Add one standard way of indenting. This should go well with the There’s Only One Way to Do It-philosophy, and should stop all these problems. I could easily settle on 2 spaces, 8 spaces or tabs, just as long as there was this single way. This won’t happen, however. There’s too much code out there, with different indents.
  2. A conventional way of commenting the chosen indent. There is already conventions used for commenting the character set among other things, and this should just be one more line to add to the template. # -*- indent: 2 -*- or # -*- indent: tab -*- should do the trick. If this convention catches on, in time editors might even learn to read this, and adjusting the indent to this value. That would be great.

These are my suggestions. I hope someone influential in the Python community sees this, and agrees with me. We need to sort this out, and this could be a way of doing this.

This is Simply Jonathan, a blog written by Jonathan Holst. It's mostly about technical topics (and mainly the Web at that), but an occasional post on clothing, sports, and general personal life topics can be found.

Jonathan Holst is a programmer, language enthusiast, sports fan, and appreciator of good design, living in Copenhagen, Denmark, Europe. He is also someone pretentious enough to call himself the 'author' of a blog. And talk about himself in the third person.