[Python Tips] PEP 8

PEP what?

PEP 8 is a coding standard for Python by Guido van Rossum. It defines a set of guidelines for writing Python code in a universally accepted writing style.

Any good developer worth their salt knows consistency is critical for development. If you use 2 spaces, use 2 spaces every time, if you use 4 do it every time. Changing your style inside of a project makes your code more difficult to read and manage.

I am not going to cover how to format your code to PEP 8 but you can read the entire guide here.

PEP8 Python Style Guide

PEP8 Style Guide Examples

A few examples of PEP 8 styles that are commonly used in most Python programs.

Line indent

PEP 8 recommends using 4 characters for indents to increase readability and consistency. This is for every indentation, not just the first one.

If you have code that will exceed the recommended 79 characters per line, this is the proper way to do a hanging indent.

foo = my_long_function_name(
    var_one, var_two,
    var_three, var_four)

While there are a few suggestions on how to format this line, the guide specifically recommends avoiding code that looks like this:

foo = my_long_function_name(var_one, var_two,
   var_three, var_four)

The most important thing is to remain consistent with yourself.

Tabs or Spaces

PEP 8 recommends using spaces instead of tabs, reverting to tabs only when working on existing code with tabs. While I am not a big fan of this rule, using a proper editor config can help solve this problem. Again, the critical rule in my opinion regardless of PEP 8 is consistency.

Blank Lines

The last example I will cover is blank lines. The guide recommends two blank lines between top-level functions and class definitions. Method definitions (functions inside of a class) should only use one line space.

The guide also recommends to use a new line to separate logic inside of functions but use this very sparingly. If you find yourself using a lot of spaces, you likely are doing too much in your function.

Enter autopep8 module

There is a module called autopep8 that will automatically format your code to autopep8 style.

You can install the module with pip install autopep8

You can run autopep8 against a single file using the following command:

$ autopep8 --in-place --aggressive --aggressive <filename>

If you are using vim, you can use this shortcut:

:%! autopep8 -

This will automatically pep8 your code in the current file. This module is really good for fixing spacing/tab inconsistencies.

The best way to enforce consistent styling is using a Linter. Mode good text editors support Python Linting. Linting will give you warnings when your code does not conform to a recommended style, either PEP 8 or the guidelines you configure it for.

I will save linting for another tip but know it exists and look at your favorite text editor for how to enable linting for your Python code.

I highly recommend reviewing the PEP 8 style guide which has been formatted version at http://pep8.org/

My Python Tips Series

X48EJ

Why you should vote me as witness

Witness & Administrator of four full nodes

themarkymark.png

My recent popular posts

STEEM, STEEM Power, Vests, and Steem Dollars. wtf is this shit?
The truth and lies about 25% curation, why what you know is FAKE NEWS
WTF is a hardware wallet, and why should you have one?
GINABOT - The Secret to your Sanity on Steemit
How to calculate post rewards
Use SSH all the time? Time for a big boy SSH Client
How to change your recovery account
How curation rewards work and how to be a kick ass curator
Markdown 101 - How to make kick ass posts on Steemit
Work ON your business, not in your business! - How to succeed as a small business
You are not entitled to an audience, you need to earn it!
How to properly setup SSH Key Authentication - If you are logging into your server with root, you are doing it wrong!
Building a Portable Game Console

H2
H3
H4
3 columns
2 columns
1 column
17 Comments
Ecency