[Python Tips] PEP 8

in #programming8 years ago

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

Sort:  

wow very nice post

Great effort and very helpful for the users, thank you The Mark O Mark

Looking really good PeP8 style writing

Ill have to check out these standards that I am sure I am not following when I get a little sleep.

Luar biasa post cantik

post a very remarkable I really like. hopefully posts I like that, too. friend help me.

I’m going to install that module. These tips on python are great! I’m working on some code right now that could use a bit of formatting ;)

I wasn't aware or pep8, thanks mate. Since I use atom as an editor I did a quick search and found a linter python pep8 plugin for atom here.. I look forward to your future tips on python.

Check out VS Code, really aggressive development progress. It's the Sublime Text we always wanted.

I would also recommend to use VSCode and you won’t never look back to Atom. I am telling you from my experience.

There are lot of plugins available which are really helpful for any kind of development.

Wow what an post about Python. I am not using though but I love python for many different reasons. First thing first it is really powerful language I ever saw. I hope I could get a chance to learn it.

@themarkymark thanks for great work.

I would recommend you please add some nice footer in your post so that reader can easily follow you.

I have designed very nice footer here. Please let me know if anyone interested for it. I can design for you as well in exchange of little SP delegation.

Thank you for sharing

Is it innapropriate to flag for differences in style preferences?

When coding by yourself, you can use any style you want but eventually, you will work with others and using a uniform style code across all devs is a huge advantage.

So you're saying it's perfectly fine to flag someone for differences in coding style...if you're working with them? :P

I mostly follow PEP8, but I think there are cases where it's better to ignore the line length.

That's a common one people have a problem with, check out this video.
It's a good video that talks about PEP 8 and is anti-forced line length and goes into reasons why and what's good as well as other things. It's a good watch.

Already watched it. :P I like him. He's a bit hard to follow when he's talking about Python sometimes though, as he's pretty advanced.