Significant whitespace

Posted by on September 18, 2008

One of the most common complains I hear from people about python is that whitespace is significant. I would have to disagree with them, I think that significant whitespace is an excellent feature in a programming language. Though only if it is implemented correctly, which I believe it is not in python.

My major complaint about python is that it allows users to mix both tabs and spaces, and also different amounts of spaces to mark a code block. I believe that python should not allow this. I think a there should be one consistent method of indenting used within a file. I would love if this method was tabs, but I don’t really mind that much if it spaces.

This problem usually occurs when you have multiple people modifying code, over an extended period of time. Each person has their own preferred style of indenting code, and will use it (generally without thinking about it).

A friend of mine recently came across this exact problem, and in an attempt to make the file “sane” used a regular expression to fix the whitespace in the file. This ended up being a major problem, as their had inadvertently changed the meaning of the code, by subtle changing the indentation levels of the code. I came up with the idea of parsing the python program, then outputting the program back from the parse tree (thus ensuring the meaning of the code is unchanged).

After being shown the compiler module, I started playing around with some code to reproduce code from the abstract parse tree that was provided to me. When I went looking for documentation, I found that there is a piece of sample code provided with the python source that does exactly what I was after (After a few bug fixes). This example is called unparse.py (located in the Demo/parser/ directory of the python 2.5.x source code).

I suggest anybody who is trying to fix inconsistent indentation in a python file to look at this program. There are a few things to note though:

  • The code must already work as wanted
  • This program will strip all comments from the program
  • The code will loose all of it’s layout - that is, code that may have been split over multiple lines, will now be over one line
Comments

Respond | Trackback

Comments

Comments: