PEP8? Or not?

From time to time someone comments on some code example I’ve produced (or, indeed, one of my published modules) and makes the point that the code is not PEP8-compliant. The most common complaints are that I use spaces before function-call brackets — spam (”foo”) rather than spam(”foo”) — and that I use 2-space indents rather than 4.

Since I’ve written pretty much the same answer whenever this has been raised, I thought I might as well write it here and then point people towards it.

PEP 8 is the Python project’s code style guideline. I quote from the opening paragraph: “This document gives coding conventions for the Python code comprising the standard library in the main Python distribution.”. It is not a guideline for every piece of Python code ever written by anyone. On the odd occasion that I contribute code to the Python core I naturally ensure that my code complies with PEPs 7 & 8 for C & Python code respectively.

I’ve been using the format described above for well over 25 years of coding on different platforms & languages. Along with other aspects, it constitutes my personal “house style”. If anyone wished to contribute to any of my projects I would ask them to follow the same guidelines (or at least, not to complain when I reformatted their code to match my own).

FWIW I find the spam(”foo”) style cramped; no doubt some people find my spam (”foo”) style too loose. So be it. I’m genuinely astonished when people advocate 8-space indents (and I’d be a bit startled by 1-space indents). I find 4 spaces push the relevant code too far away too easily so I use 2 spaces. Barring manifest absurdities, such things are largely subjective.

So now you know: if you see code examples of mine, or are perusing my source code, be prepared for (gasp) non-PEP8 compliance!