Python’s style guide: the good, the bad, and the ugly

I’ve been looking recently at making some contributions to a project that uses python for computational mathematics. This, of course, meant that I had to look up some tutorials on python! Having never done much with the language before, it should be an educational experience. One of the points the official tutorial brings up is the most popular style guide for python projects. There’s plenty there that I think is totally awesome, but there are also some guidelines that I hope I’ll never stoop low enough to follow.

The good: Extraneous whitespace is the biggie here. It’s a huge pet peeve of mine when people think they have to drown every set of parentheses in spaces, and this style guide recommends against it. Bravo. Comments should also be complete sentences — I completely agree with that. Incoherent babbling with an octothorpe at the beginning never did anyone any good. Limiting line length is also good, although it seems to be pretty standard practice. It’s good to explicitly state it though. Finally, the guideline about putting separate statements on separate lines is a good one, but again, pretty much standard practice.

The bad: In comments, this guide says to use two spaces after a period that ends a sentence. There are so many reasons why not to do this. Admittedly, programming editors most often use a monospace font, so this might be a bit more of a gray area, but I find single-spaced text to be easier to read even in a monospace font anyway. Regardless, it’s a bad habit to get into, especially in a field like Computer Science where modern technology and trends matter more.

The ugly: This is a matter of personal preference, but I disagree with the recommendation to use tabs, rather than spaces, for indentation. Spaces force one way of looking at code on anybody who reads it, as well as being extremely easy to do wrong. Having a space too many or too few on a line is very common in code I’ve seen that uses spaces for indentation. The “tabs vs. spaces” argument is one for another day, but I felt I had to mention it here because of its (biased) inclusion in the style guide.

To summarize, the official style guide that the developers of python recommend has a lot of very good points, but falls short in a few crucial areas. While many of the practices they recommend are verifiably sound, there are other points that should be rethought, or at the very least, the reasoning behind them should be clarified.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.