There are two things I think are just plain wrong. Actually there are millions, but there are two I have in mind right now. The first is spaces instead of tab characters for indenting code. The second also involves spaces – excessive use of them like 1 + 1 = 2 when 1+1=2 is, to me, far more sensible. I don’t want to argue about either of these things, because a) this is a religious matter, i.e. I’m right and you’re wrong, and b) there’s no point, because you win anyway.
Why? Because in the interests of global harmony, code sharing, encouraging of contributions, etc, all my Python projects will now follow the PEP 8 guidelines. This doesn’t affect my contributions to other projects, since they’re always in the style required by that project (consistency always trumps religion), but it does mean that others can use and contribute to my stuff more easily, and hopefully in time I’ll get so used to seeing 1 + 1 = 2 that it won’t look stupid any more.
So, on the subject of PEP 8, a couple of tips I figured out along the way:
Fixing Indentation
There are a few ways to handle this, but the simplest and most reliable seems to be an example that’s part of the Python distribution itself. On Debian-based systems, this script is packaged separately in python-examples. Re-indenting a file to PEP 8 standards in that environment can be done like this:
python /usr/share/doc/python2.6/examples/Tools/scripts/reindent.py yourfile.py
Checking Code
There’s a handy utility, aptly named pep8, for checking code against the PEP 8 guidelines. The simplest way to install it is:
easy_install pep8
After that, you can just run
pep8 *.pyto get a list of your crimes against style and readability. Note that, in the current release at least, it doesn’t seem to do a perfect job. For example, this is flagged as an error:
value = func(-x, -y)
It doesn’t like that fact that there are no spaces around the unary minus, which I think is wrong. Moreover, it seems to be impossible to write that code in a way that it does like – if you put spaces, it complains about that instead.

No comments
Comments feed for this article
Trackback link: http://ciarang.com/posts/python-over-to-the-dark-side/trackback