This Weeks Obsession: PEP 8
One of the reasons that I was initially attracted to Python is that it is beautiful.
If you’ve never stared at lines of cryptic Perl and scratched your head trying figure out what is even going on, you might not understand this.
If you have never gronked through hundreds of lines of html that works but was written by someone that has never heard of semantics or a validator; I’ll forgive you for thinking my opening sentence is hyperbolic.
For those of you that understand that code is poetry, read on…
Over the last month or so, with the help of the lovely Teresa Gail, I have been able to devote copious amounts of time to studying Python. I am proficient but I want to be great!
To this end I have spent days immersed in Python. When I could code no longer I was watching PyCon videos. Bed time reading has been learning about obscure libraries and multi-threading.
In addition to all this I have of course been working through Python classes, and… getting into PEP 8.
Python Enhancement Proposals, or PEPs, are documents that describe new features and guidelines proposed for Python. These documents cover all sorts of issues. For instance the latest, 8016, covers the Steering Council Model…
The most famous, and important PEP is PEP 8.
PEP 8 is the Python style guide written primarily by the creator of Python Guido van Rossum. PEP 8 defines “coding conventions for the Python code comprising the standard library in the main Python distribution.” It is really the officially sanctioned guide to formatting the text that comprises your Python code.
It’s not surprising that a programming language that declares “Beautiful is better than ugly” has a style guide. The rationale for an official guide, Mr. van Rossom (rightly) explained, is that “Code is read much more often than it is written.” Therefore readability is hugely important. PEP 8 endeavors to define a style for readable code.
Initially PEP 8 felt constraining and I wanted to do things the way I had always done things. I started typing code into notepad in the early nineties, I have developed my own style guide. However in my continuing commitment to being an old dog that can learn new tricks I have been trying to write code that is more PEP compliant, and therefore more Pythonic.
I can get with most of PEP 8 but I am never going to indent with 4 spaces. I am a tab man, through and through. I can only compromise so far. Thankfully there are ide’s and linters out there that will convert my tabs to spaces to appease the PEP 8 Nazi’s.
Interestingly enough Google has their own Python style guide. Disappointingly Google prefer spaces as well…
No matter what language you code in you can get some wisdom by reading PEP 8. PEP 8 combined with PEP 20 combine to create something just shy of a religion for some people. If you give the principles a chance they can at least change how you approach writing code. Although it may not be obvious at first, unless you’re Dutch.