Python style guide Flashcards
indentation alligment of ( ), [ ], { }
# Aligned with opening delimiter. foo = long_function_name(var_one, var_two, var_three, var_four)
# More indentation included to distinguish this from the rest. def long_function_name( var_one, var_two, var_three, var_four): print(var_one)
# Hanging indents should add a level. foo = long_function_name( var_one, var_two, var_three, var_four)
indentation (basic)
4 spaces (try not to use tab)
indentation for if statements when statement leaves no room for parenthesis
No extra indentation.
if (this_is_one_thing and
that_is_another_thing):
do_something()
Add a comment, which will provide some distinction in editors
# supporting syntax highlighting.
if (this_is_one_thing and
that_is_another_thing):
# Since both conditions are true, we can frobnicate.
do_something()
Add some extra indentation on the conditional continuation line.
if (this_is_one_thing
and that_is_another_thing):
do_something()
indentation: The closing brace/bracket/parenthesis on multi-line constructs may either line up under the first non-whitespace character of the last line of list.
my_list = [ 1, 2, 3, 4, 5, 6, ] result = some_function_that_takes_arguments( 'a', 'b', 'c', 'd', 'e', 'f', )
or
my_list = [ 1, 2, 3, 4, 5, 6, ] result = some_function_that_takes_arguments( 'a', 'b', 'c', 'd', 'e', 'f', )
maximum line length
Limit all lines to a maximum of 79 characters.
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
Imports should be on separate lines
Yes: import os
import sys
No: import sys, os
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
Imports should be grouped in the following order:
standard library imports
related third party imports
local application/library specific imports
You should put a blank line between each group of imports.
Imports- absolutes
Absolute imports are recommended, as they are usually more readable and tend to be better behaved (or at least give better error messages) if the import system is incorrectly configured.
import mypkg.sibling
from mypkg import sibling
from mypkg.sibling import example
explicit relative imports
explicit relative imports are an acceptable alternative to absolute imports, especially when dealing with complex package layouts where using absolute imports would be unnecessarily verbose:
from . import sibling
from .sibling import example
importing classes
When importing a class from a class-containing module, it’s usually okay to spell this:
from myclass import MyClass from foo.bar.yourclass import YourClass
If this spelling causes local name clashes, then spell them
import myclass import foo.bar.yourclass
and use “myclass.MyClass” and “foo.bar.yourclass.YourClass”.
whitespace
Avoid extraneous whitespace in the following situations:
Immediately inside parentheses, brackets or braces.
Immediately before a comma, semicolon, or colon
Immediately before the open parenthesis that starts the argument list of a function call
Immediately before the open parenthesis that starts an indexing or slicing
More than one space around an assignment (or other) operator to align it with another.
always surround binary operators with single space. operators style i = i + 1 submitted += 1 x = x*2 - 1 hypot2 = x*x + y*y c = (a+b) * (a-b)
comments
You should use two spaces after a sentence-ending period.
PEP 257 describes good docstring conventions. Note that most importantly, the “”” that ends a multiline docstring should be on a line by itself
“"”Return a foobang
Optional plotz says to frobnicate the bizbaz first.
“””