Submit Blog  RSS Feeds

Thursday, January 31, 2013

Python code readability - PEP 8

Everybody familiar with python should be aware there are a bunch of documents called PEPs (Python Enhancement Proposals). As the name states, this documents are intended to help improve python, not only by adding new features to the interpreter and enhancing standard libraries, they also give guidelines (proposals) about meta-programming. Many experienced and respectable members of the python community participate in the PEP life-cycle, thus the documents are really reliable.

The thing I would like to talk about is PEP 8, also known as the Style Guide for Python Code.
As you may know, the proposed name convention differs a bit from other high level programming languages (like Java, whitch its known for UlitimateLongAndDescriptiveClassName naming convetion, along with other funny things like evenLongerAndMoreSpohisticatedMethodNames). Anyway it's very intuitive, 4 spaces instead of tab, underscore inside multi-word variables, etc. Sounds cool, you may even download a package that checks your type convention with reference to PEP 8:

~ $ pip install pep8

Now you may check your perfectly compatible with PEP 8 source codes:

~ $ pep8 app/

and to your wonderment get a result similar to this:

app/ E128 continuation line under-indented for visual indent
app/ E502 the backslash is redundant between brackets
app/ W293 blank line contains whitespace
app/ E303 too many blank lines (5)
app/ E501 line too long (82 > 79 characters)
app/ E501 line too long (95 > 79 characters)
app/ E501 line too long (103 > 79 characters)
app/ E501 line too long (82 > 79 characters)
app/ E501 line too long (83 > 79 characters)
app/ E302 expected 2 blank lines, found 1
app/ W391 blank line at end of file

A clean and tidy source file prints so many errors... well yes it does. In fact without additional IDE features it's nearly impossible to write PEP 8 valid code. If you're using vim, you can get a PEP 8 validation plug-in that opens a quick-fix buffer with a list of PEP 8 incompatible statements. This is good for shaping your coding habits, but don't get to orthodox - don't ever change an existing projects coding convention, just keep to the current one.

~Thus spoketh KR,


  1. Hi,

    Try syntastic+vim+pylint (pep8 and more)..

    1. Thanks for the comment, syntastic+pylint looks like a nice setup. However as far as I remember the default pylintrc configuration is a bit harsh.


free counters