0% found this document useful (0 votes)
29 views

Jmds Python Pep8 Style Guide

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Jmds Python Pep8 Style Guide

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python PEP8 style guide Cheat Sheet

by jmds via cheatography.com/84942/cs/20012/

Naming conven​tions Comments

Never use l, O, or I single letter names as O = 2 # This may look Limit the line length of Use complete Make sure to update
these can be mistaken for 1 and 0, like you're trying to comments and sentences, starting comments if you
depending on typeface reassign 2 to zero docstrings to 72 with a capital change your code.

Function function, my_fun​ction charac​ters. letter.

Variable x, var, my_var​iable


Block Comments
Class Model, MyClass
Indent block comments Start each line Separate
Method class_​method, method
to the same level as the with a # followed paragraphs by a
Constant CONSTANT, MY_CON​‐ code they describe. by a single space. line containing a
STANT, MY_LON​G_C​‐ single #.
ONSTANT
Module module.py, my_mod​‐ Inline Comments
ule.py
Use inline comments sparingly.
Package package, mypackage
Write inline comments on the same line as the statement they refer
to.
Maximum Line Length and Line Breaking
Separate inline comments by two or more spaces from the
PEP 8 suggests lines should be limited to 79 statement.
characters. This is because it allows you to have
Start inline comments with a # and a single space, like block
multiple files open next to one another, while
comments.
also avoiding line wrapping.
Don’t use them to explain the obvious.
Python will assume line contin​uation if code is
contained within parent​heses, brackets, or
When to Avoid Adding Whitespace
braces:
The most important place to avoid adding whitespace is at the end of
def functi​on(​arg​_one, arg_two,
a line. This is known as trailing whitespace
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​arg​_three, arg_four):
​ ​ ​ ​return arg_one Immedi​ately inside parent​heses, brackets, or braces:

If it is impossible to use implied contin​uation, Before a comma, semicolon, or colon:


then you can use backsl​ashes to break lines Before the open parent​hesis that starts the argument list of a function
instead: call:
from mypkg import example1, \ Before the open bracket that starts an index or slice:
​ ​ ​ ​exa​mple2, example3
Between a trailing comma and a closing parent​hesis:
# Recomm​ended
To align assignment operators:
total = (first​_va​riable
immedi​ately inside brackets, as well as before commas and colons.
​ ​ ​ ​ ​ ​ ​ ​ + second​_va​riable
​ ​ ​ ​ ​ ​ ​ ​ - third_​var​iable)
Progra​mming Recomm​end​ations
# Not Recomm​ended
total = (first​_va​riable + Don’t compare boolean values to Use the fact that empty

​ ​ ​ ​ ​ ​ ​ ​ ​sec​ond​_va​riable - True or False using the equiva​lence sequences are falsy in if


operator. statem​ents.
​ ​ ​ ​ ​ ​ ​ ​ ​thi​rd_​var​iable)
Use is not rather than not ... is in if Don’t use if x: when you
Identation statem​ents. mean if x is not None:

Use 4 consec​utive spaces to indicate indent​ation. Use .start​swith() and .endsw​ith() instead of slicing.

Prefer spaces over tabs.

By jmds Not published yet. Sponsored by ApolloPad.com


cheatography.com/jmds/ Last updated 10th July, 2019. Everyone has a novel in them. Finish
Page 1 of 3. Yours!
https://apollopad.com
Python PEP8 style guide Cheat Sheet
by jmds via cheatography.com/84942/cs/20012/

Code Layout Where to Put the Closing Brace (cont)

Surround top-level functions and classes with two blank lines > ​​​]
Surround method defini​tions inside classes with a single blank line. 2 - Line up the closing brace with the first character of the line that
starts the construct:
Use blank lines sparingly inside functions to show clear steps.
list_o​f_n​umbers = [
​ ​ ​ 1, 2, 3,
Indent​ation Following Line Breaks
​ ​ ​ 4, 5, 6,
There are two styles of indent​ation you can use. ​ ​ ​ 7, 8, 9
The first of these is to align the indented block with the opening ]
delimiter:
An altern​ative style of indent​ation following a line break is a hanging Docume​ntation Strings
indent. This is a typogr​aphical term meaning that every line but the Surround docstrings with three double quotes on either side, as in
first in a paragraph or statement is indented. "​"​"This is a docstr​ing​"​"​".
Write them for all public modules, functions, classes, and methods.
Indent​ation Following Line Breaks 2
Put the "​"​" that ends a multiline docstring on a line by itself:
def function(arg_one, arg_two,
For one-line docstr​ings, keep the "​"​" on the same line:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​arg​_three, arg_four):
​ ​ ​ ​return arg_one
Whitespace Around Binary Operators
x = 5
Surround the following binary operators with a single space on either
if (x > 3 and
side:
​ ​ ​ x < 10):
Assignment operators (=, +=, -=, Compar​isons (==, Booleans
​ ​ ​ ​pri​nt(x)
and so forth) !=, >, <. >=, <=) and (and, not,
x = 5
(is, is not, in, not in) or)
if (x > 3 and
​ ​ ​ x < 10): When = is used to assign a def functi​on(​def​aul​t_p​ara​met​‐
default value to a function er=5):
​ ​ ​ # Both conditions satisfied
argument, do not surround it with
​ ​ ​ ​pri​nt(x)
spaces.
x = 5
if (x > 3 and y = x**2 + 5 z = (x+y) * (x-y)

​ ​ ​ ​ ​ ​ ​ x < 10): if x>5 and x%2==0:


​ ​ ​ ​pri​nt(x) # Treat the colon as the operator with lowest priority
# hanging indent list[x+1 : x+2]
var = function(
In an extended slice, both colons must be surrounded by the same
​ ​ ​ ​arg​_one, arg_two,
amount of whitespace
​ ​ ​ ​arg​_three, arg_four)
list[3​:4:5] list[x+1 : x+2 : x+3]
The space is omitted if a slice list[x+1 : x+2 :]
Where to Put the Closing Brace
parameter is omitted
PEP 8 provides two options for the position of the
closing brace in implied line continuations:
1 - Line up the closing brace with the first non-
wh​ite​space character of the previous line:
list_o​f_n​umbers = [
​ ​ ​ 1, 2, 3,
​ ​ ​ 4, 5, 6,
​ ​ ​ 7, 8, 9

By jmds Not published yet. Sponsored by ApolloPad.com


cheatography.com/jmds/ Last updated 10th July, 2019. Everyone has a novel in them. Finish
Page 2 of 3. Yours!
https://apollopad.com
Python PEP8 style guide Cheat Sheet
by jmds via cheatography.com/84942/cs/20012/

When to Ignore PEP 8

If complying with PEP 8 would break compat​ibility with existing


software
If code surrou​nding what you’re working on is incons​istent with PEP
8
If code needs to remain compatible with older versions of Python

By jmds Not published yet. Sponsored by ApolloPad.com


cheatography.com/jmds/ Last updated 10th July, 2019. Everyone has a novel in them. Finish
Page 3 of 3. Yours!
https://apollopad.com

You might also like