Snippets
Python style guide based on PEP8 guidelines
Indentation
Use 4 spaces per indentation level.
Sample
def myfunc():
my_list = [
1, 2, 3,
4, 5, 6
]
Line length
Limit most lines to a maximum of 79 characters. For long lines wrap the expression inside parens.
Sample
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
raise ValueError('I don't think so -- values are {}, {}, {}.format(
width, height))
Blank lines
- Separate top-level function and class definitions with two blank lines.
- Method definitions inside a class are separated by a single blank line.
- Imports should be put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
- Imports should be on separate lines.
- Imports should be grouped in the following order:
- Standard library imports
- Related third party imports
- Local application/library specific imports
- Put a blank line between each group of imports
- Put any relevant _all_ specification after the imports
- Use absolute imports like os.path
Sample
import os
import os.path
import third_party_lib
import third_party_lib2
import mylib
import mylib2
class Cookie:
def foo:
pass
def bar:
pass
class Cake:
pass
White space
No extra white spaces in the following situations:
- Immediately inside parens, brackets, or braces
- Immediately before a comma, semicolon, or colon
- Immediately before the open parens that starts the argument list of a function call
- Immediately before the open parens that starts an indexing or slicing
- More than one space around an assignment (or other) operator to align it with another
Surround these binary operators with a single space on either side:
- assignment: =
- augmented assignment: +=, -=, etc.
- comparisons: ==, <, >, !=, <>, <=, >=, in, not in, is, is not
- booleans: and, or, not
- arithmetic: add white space around the operator with the lowest priority
No spaces around the equal sign when used to indicate a keyword argument or a default parameter value.
Samples
func(ary[1], {key: val}) # yes
func( ary[ 1 ], { key: val }) # no
if x == 4: print(x, y) # yes
if x == 4: print(x , y) # no
x, y = y, x # yes
x , y = y , x # no
func(val) # yes
func( val ) # no
hsh[key] = ary[ndx] # yes
hsh [key] = ary [ndx] # no
# yes
x = 1
y = 2
long_variable = 3
# no
x = 1
y = 2
long_variable = 3
# yes
i = i + j
submitted += 1
x = 2*x - 1
h = x*x + y*y
c = (a+b) * (a-b)
# no
i=i+j
submitted +=1
x = 2 * x - 1
h = x * x + y * y
c = (a + b) * (a - b)
# yes
def complex(real, imag=0.0):
return magic(r=real, i=imag)
# no
def complex(real, imag = 0.0):
return magic(r = real, i = imag)
Naming conventions
mypackage
my_module
ClassName
CustomExceptionError
funciton_name
def instance_method_name(self)
def class_method_name(cls)
_protected_instance_method
_protected_instance_attr
__private_instance_attr
MY_CONST_VAR
Docstrings
Use triple quotes with each quote being a double quote at the begining of each module, class, method, or function. For internal modules, it is ok to simply add a doc string at the module and skip documenting each and every function in the module. The title of the docstring should be in the same line as the opening quotes, followed by a blank line, followed by detailed documentation.
def my_func():
"""Purpose of this function
Some more details of what the function does
why this approach, etc. But for the sake of
everything good and holy don't just translate
the code line-by-line in English.
"""
do_something()
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.