create account

[Python Tips] PEP 8 by themarkymark

View this thread on: hive.blogpeakd.comecency.com
· @themarkymark ·
$103.45
[Python Tips] PEP 8
![](https://steemitimages.com/DQmSsFP8BDE1DkXNN9hh1729MRexyqupLEKfWJTsRskjt33/image.png)

# 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](http://pep8.org/). 

### PEP8 Python Style Guide

https://www.youtube.com/watch?v=PBU1SJlWHeQ

# 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

https://www.youtube.com/watch?v=SsoOG6ZeyUI

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](http://editorconfig.org/) 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
* [f-strings in Python 3.6](https://steemit.com/programming/@themarkymark/python-tips-f-strings-in-python-3-6-and-why-you-should-be-using-them)
* [Underscores in numeric literals](https://steemit.com/programming/@themarkymark/python-tips-underscores-in-numeric-literals)
* [A better interactive shell](https://steemit.com/programming/@themarkymark/python-tips-a-better-interactive-shell)
* [Secrets Module - New in 3.6](https://steemit.com/programming/@themarkymark/python-tips-secrets-module-new-in-3-6)


<center><sub><sub> X48EJ </sub></sub></center>

# <center>  [Why you should vote me as witness](https://steemit.com/witness-category/@themarkymark/why-you-should-vote-for-themarkymark-as-witness)  </center> #
<center>https://steemitimages.com/DQmcWxV1dpA1eAtw2ipwZiWZkydyVNU5LaLa2Ak1GUnbGmS/The-Marky-Mark.png </center>

# <center> Witness & Administrator of four full nodes </center> #

![themarkymark.png](https://steemitimages.com/DQmVpwMxWTYDVum2dXcVRBLwHqHZh2Sr6DDHAFwDeeTG2YK/themarkymark.png)


</br>

# <center> My recent popular posts </center> #

[STEEM, STEEM Power, Vests, and Steem Dollars. wtf is this shit?](https://steemit.com/steem/@themarkymark/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](https://steemit.com/curation/@themarkymark/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?](https://steemit.com/bitcoin/@themarkymark/wtf-is-a-hardware-wallet-and-why-should-you-have-one)
[GINABOT - The Secret to your Sanity on Steemit](https://steemit.com/ginabot/@themarkymark/ginabot-the-secret-to-your-sanity-on-steemit)
[How to calculate post rewards](https://steemit.com/steemit/@themarkymark/how-to-calculate-post-rewards)
[Use SSH all the time? Time for a big boy SSH Client](https://steemit.com/linux/@themarkymark/use-ssh-all-the-time-time-for-a-big-boy-ssh-client)
[How to change your recovery account](https://steemit.com/steemit/@themarkymark/how-to-change-your-recovery-account)
[How curation rewards work and how to be a kick ass curator](https://steemit.com/curation/@themarkymark/how-curation-rewards-work-and-how-to-be-a-kick-ass-curator)
[Markdown 101 - How to make kick ass posts on Steemit](https://steemit.com/steemit/@themarkymark/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](https://steemit.com/business/@themarkymark/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!](https://steemit.com/steemit/@themarkymark/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!](https://steemit.com/sysadmin/@themarkymark/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](https://steemit.com/raspberrypi/@themarkymark/building-a-portable-game-console)

[![](https://steemitimages.com/DQmPkQhAQceC7aHr4Gy5GKv7LMoiy47P7PyaNCeZBHMpFke/white-bg-with-drop-shadow.gif)](https://v2.steemconnect.com/sign/account-witness-vote?witness=themarkymark&approve=1) 
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 59 others
properties (23)
authorthemarkymark
permlinkpython-tips-pep-8
categoryprogramming
json_metadata{"community":"busy","app":"busy/2.4.0","format":"markdown","tags":["programming","python","python-tips","dev","busy"],"users":["themarkymark"],"links":["http://pep8.org/","http://editorconfig.org/","http://pep8.org/","https://steemit.com/programming/@themarkymark/python-tips-f-strings-in-python-3-6-and-why-you-should-be-using-them","https://steemit.com/programming/@themarkymark/python-tips-underscores-in-numeric-literals","https://steemit.com/programming/@themarkymark/python-tips-a-better-interactive-shell","https://steemit.com/programming/@themarkymark/python-tips-secrets-module-new-in-3-6","https://steemit.com/witness-category/@themarkymark/why-you-should-vote-for-themarkymark-as-witness","https://steemit.com/steem/@themarkymark/steem-steem-power-vests-and-steem-dollars-wtf-is-this-shit","https://steemit.com/curation/@themarkymark/the-truth-and-lies-about-25-curation-why-what-you-know-is-fake-news"],"image":["https://steemitimages.com/0x0/https://steemitimages.com/DQmSsFP8BDE1DkXNN9hh1729MRexyqupLEKfWJTsRskjt33/image.png","https://steemitimages.com/0x0/https://steemitimages.com/DQmcWxV1dpA1eAtw2ipwZiWZkydyVNU5LaLa2Ak1GUnbGmS/The-Marky-Mark.png","https://steemitimages.com/0x0/https://steemitimages.com/DQmVpwMxWTYDVum2dXcVRBLwHqHZh2Sr6DDHAFwDeeTG2YK/themarkymark.png","https://steemitimages.com/0x0/https://steemitimages.com/DQmPkQhAQceC7aHr4Gy5GKv7LMoiy47P7PyaNCeZBHMpFke/white-bg-with-drop-shadow.gif"]}
created2018-04-15 09:04:00
last_update2018-04-15 09:04:00
depth0
children17
last_payout2018-04-22 09:04:00
cashout_time1969-12-31 23:59:59
total_payout_value94.431 HBD
curator_payout_value9.023 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,656
author_reputation1,779,919,634,875,089
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,165,335
net_rshares20,182,213,808,074
author_curate_reward""
vote details (123)
@akmal1 ·
post a very remarkable I really like. hopefully posts I like that, too. friend help me.
properties (22)
authorakmal1
permlinkre-themarkymark-python-tips-pep-8-20180415t092650617z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-04-15 09:27:00
last_update2018-04-15 09:27:00
depth1
children0
last_payout2018-04-22 09:27:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length87
author_reputation760,367,642,451
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,168,028
net_rshares0
@dpakyaw ·
Thank you for sharing
properties (22)
authordpakyaw
permlinkre-themarkymark-python-tips-pep-8-20180416t034424868z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-04-16 03:44:24
last_update2018-04-16 03:44:24
depth1
children0
last_payout2018-04-23 03:44:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length21
author_reputation4,041,738,037,502
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,306,951
net_rshares0
@geekpowered ·
Is it innapropriate to flag for differences in style preferences?
properties (22)
authorgeekpowered
permlinkre-themarkymark-python-tips-pep-8-20180416t162319768z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-04-16 16:23:21
last_update2018-04-16 16:23:21
depth1
children4
last_payout2018-04-23 16:23:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length65
author_reputation99,319,261,112,097
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,408,976
net_rshares0
@themarkymark ·
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.
properties (22)
authorthemarkymark
permlinkre-geekpowered-re-themarkymark-python-tips-pep-8-20180416t173022284z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-04-16 17:30:03
last_update2018-04-16 17:30:03
depth2
children3
last_payout2018-04-23 17:30:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length165
author_reputation1,779,919,634,875,089
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,419,041
net_rshares0
@geekpowered ·
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.
properties (22)
authorgeekpowered
permlinkre-themarkymark-re-geekpowered-re-themarkymark-python-tips-pep-8-20180417t015714331z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-04-17 01:57:15
last_update2018-04-17 01:57:15
depth3
children2
last_payout2018-04-24 01:57:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length214
author_reputation99,319,261,112,097
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,481,019
net_rshares0
@iamredbar ·
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 ;)
πŸ‘  
properties (23)
authoriamredbar
permlinkre-themarkymark-python-tips-pep-8-20180415t130817445z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-04-15 13:08:18
last_update2018-04-15 13:08:18
depth1
children0
last_payout2018-04-22 13:08:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length138
author_reputation5,765,586,647,876
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,195,492
net_rshares0
author_curate_reward""
vote details (1)
@jakboy ·
wow very nice post
properties (22)
authorjakboy
permlinkre-themarkymark-python-tips-pep-8-20180415t090732076z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-04-15 09:07:30
last_update2018-04-15 09:07:30
depth1
children0
last_payout2018-04-22 09:07:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length18
author_reputation45,413,276,305
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,165,711
net_rshares0
@juliawilliams ·
Great effort and very helpful for the users, thank you The Mark O Mark
properties (22)
authorjuliawilliams
permlinkre-themarkymark-python-tips-pep-8-20180415t090829348z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-04-15 09:08:33
last_update2018-04-15 09:08:33
depth1
children0
last_payout2018-04-22 09:08:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length70
author_reputation12,429,776,429,321
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,165,814
net_rshares0
@molynar ·
Luar biasa post cantik
properties (22)
authormolynar
permlinkre-themarkymark-2018415t161242171z
categoryprogramming
json_metadata{"tags":["programming","python","python-tips","dev","busy"],"app":"esteem/1.6.0","format":"markdown+html","community":"esteem"}
created2018-04-15 09:12:57
last_update2018-04-15 09:12:57
depth1
children0
last_payout2018-04-22 09:12:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length22
author_reputation637,681,558,979
root_title"[Python Tips] PEP 8"
beneficiaries
0.
accountesteemapp
weight1,000
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,166,347
net_rshares0
@nicnas ·
Ill have to check out these standards that I am sure I am not following when I get a little sleep.
properties (22)
authornicnas
permlinkre-themarkymark-python-tips-pep-8-20180415t091149633z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-04-15 09:11:48
last_update2018-04-15 09:11:48
depth1
children0
last_payout2018-04-22 09:11:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length98
author_reputation38,554,121,369,241
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,166,207
net_rshares0
@synergysteem · (edited)
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 <a href="https://atom.io/packages/linter-python-pep8" >here.</a>.  I look forward to your future tips on python.
properties (22)
authorsynergysteem
permlinkre-themarkymark-python-tips-pep-8-20180415t134446264z
categoryprogramming
json_metadata{"tags":["programming"],"links":["https://atom.io/packages/linter-python-pep8"],"app":"steemit/0.1"}
created2018-04-15 13:44:48
last_update2018-04-15 13:45:27
depth1
children2
last_payout2018-04-22 13:44:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length248
author_reputation40,909,575,209
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,200,871
net_rshares0
@techstack · (edited)
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.
properties (22)
authortechstack
permlinkre-synergysteem-re-themarkymark-python-tips-pep-8-20180415t152232248z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-04-15 15:22:33
last_update2018-04-15 15:23:15
depth2
children0
last_payout2018-04-22 15:22:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length203
author_reputation745,033,069,601
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,216,518
net_rshares0
@themarkymark · (edited)
Check out VS Code, really aggressive development progress.  It's the Sublime Text we always wanted.
πŸ‘  
properties (23)
authorthemarkymark
permlinkre-synergysteem-re-themarkymark-python-tips-pep-8-20180415t140544368z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-04-15 14:05:30
last_update2018-04-15 14:05:45
depth2
children0
last_payout2018-04-22 14:05:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length99
author_reputation1,779,919,634,875,089
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,204,073
net_rshares580,540,668
author_curate_reward""
vote details (1)
@techstack ·
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](https://steemit.com/steem/@techstack/style-your-steem-with-stylish-footer). Please let me know if anyone interested for it. I can design for you as well in exchange of little SP delegation.
properties (22)
authortechstack
permlinkre-themarkymark-python-tips-pep-8-20180415t151956348z
categoryprogramming
json_metadata{"tags":["programming"],"users":["themarkymark"],"links":["https://steemit.com/steem/@techstack/style-your-steem-with-stylish-footer"],"app":"steemit/0.1"}
created2018-04-15 15:19:57
last_update2018-04-15 15:19:57
depth1
children0
last_payout2018-04-22 15:19:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length571
author_reputation745,033,069,601
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,216,093
net_rshares0
@usman119 ·
Looking really good PeP8 style writing
properties (22)
authorusman119
permlinkre-themarkymark-python-tips-pep-8-20180415t071551845z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-04-15 09:09:18
last_update2018-04-15 09:09:18
depth1
children0
last_payout2018-04-22 09:09:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length38
author_reputation9,777,746,754,968
root_title"[Python Tips] PEP 8"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id50,165,914
net_rshares0