Writing PEP 8 Compliant Code

Chaz Frazer
4 min readApr 5, 2021

--

Get your Pythonese Writing Style On

Writing concise, readable, and clean code can be a great habit to have, and just like any professional and academic writing style, has its own ‘unofficial’ rules and guidelines. Although Python and IDEs are very lenient in how code is interpreted — allowing for personal expression in individuals’ coding—writing in some sort of regulated Pythonic is preferred. Here, we’ll go over a few examples of keeping your python code blocks as PEP 8 compliant as possible.

Ever have that boss that has pet peeves about almost everything? From graph types to colors down to the font size of your work? Keeping your code super-clean is one way to auto-ignore their incessant nagging. The PEP 8 Style Guide is a great reference on how to get started and build some good habits in formatting and readability so that you (or your particular IDE) don’t get confused. Some IDEs can be finicky about simple things such as trailing whitespaces and unnecessary indentation more than others.

Example of non-compliant code with trailing operands

The above example shows how trailing operands could impact code readability. The operands are scattered on different vertical planes due to the character length of the variables. A simple fix would be to:

Lining up operands vertically creates neat, easy-to-read code. PEP compliant YAY!

The style guide is chock full of best practices and useful tidbits to make your code as pretty as possible, and perfect for obsessive people like myself (just one more thing to be neurotic about…).

Other great tips include styling for imports (where they should and shouldn't go) and creating a hierarchy and groupings based on the types of imports being made.

Separating import types and specifying a hierarchy just makes sense for my OCD.

If you’ve worked with me, you might have realized that I’m kinda obsessed with organizing(notebooks, class notes, styling, themes, etc.). It just keeps me sane when dealing with the sheer amount of information that comes our way in class. Trust me that going over this style guide will help you get better organized on some level, and help you immensely when debugging your own code (or someone else’s/vice-versa).

This is A LOT. Isn’t there some sort of cheat-sheet or shortcut?

Fortunately, you shouldn’t have to memorize this word-for-word at this point, especially with everything else we have going on day-to-day. Jupyter has a wonderful customizer in Nbextentions (an unofficial extension manager notebooks — a la Chrome/Firefox extensions).

I highly recommend reading through this wonderful blog by Will Koehrsen, which dives into installation and also goes over a few of the most useful extensions available.

Variable Inspector is another super-handy extension!

When it comes to PEP 8 compliance, however, there’s an extension for that as well! The Autopep8 extension will take your messy jumble of code and format it automatically for you! This is on a cell-by-cell basis so don’t get too excited thinking your entire notebook is gonna get magically Marie Kondo’d for you (would be nice tho…).

Autopep8. Like clutter cleanup but way less satisfying.

Conclusion

Keep it clean and simple people…

To get your PEP 8 compliance scout badge, take a look at the guideline book linked below, and give Will’s blog a once over; he goes into much more detail about other things Nbextensions can do to make your life easier. We’ve seen varying styles of coding so far from our peers and instructors already, and while everyone has their own take on their personal writing style, it's always good to keep something like this in the back of your mind. You know, just in case your future boss is a formatting nazi like good old Richard Hendricks.

--

--

Chaz Frazer
Chaz Frazer

Written by Chaz Frazer

Data Scientist/Esports Analyst/Linguist/Japanese Interpreter: I do a lot in the intersection of gaming and data, helping esports grow.

No responses yet