Skip to content

Instantly share code, notes, and snippets.

@1337
Created April 5, 2012 19:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1337/2313574 to your computer and use it in GitHub Desktop.
Save 1337/2313574 to your computer and use it in GitHub Desktop.
So many things to hate about PEP 8
#!/usr/bin/env python
# PEP 20 supersedes PEP 8 http://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds
# Follow PEP 20 if readability is affected by PEP 8
# max length 79 chars, not 80 (http://www.python.org/dev/peps/pep-0008/#maximum-line-length)
"""One-line summary of purpose. # double quoted docstrings; max length 72 chars (http://www.python.org/dev/peps/pep-0008/#maximum-line-length)
# 2nd line must be blank http://www.python.org/dev/peps/pep-0257/#multi-line-docstrings
More things # aligns with the quotes
More things
""" # end quotes should be on separate line, unless whole docstring is one line http://www.python.org/dev/peps/pep-0257/#multi-line-docstrings
__version__ = "$Revision: dc5a21b8bc47 $" # version tagging with SVN (not currently used) http://www.python.org/dev/peps/pep-0008/#version-bookkeeping
# $Source$ # version tagging with SVN (not currently used)
import standard library 1 # import sequences, styles http://www.python.org/dev/peps/pep-0008/#imports
import std library 2 # different libraries on different lines (sorted, always on top of file)
# NO aligning
# blank line between groups of imports
from standard library 3 import class 1, class 2, class 3 # same library on same line (sorted)
from std library 4 import class 4, class 5, class 6 # from... lines are sorted by the things they import. NO aligning
# 2 lines above classes AND TOP LEVEL FUNCTIONS http://www.python.org/dev/peps/pep-0008/#blank-lines
class ChildClass(Parent): # no spaces before (, after (, before ), and after ) http://www.python.org/dev/peps/pep-0008/#pet-peeves
"""Class does blah.""" # no padding spaces in docstrings; first line ends with period http://www.python.org/dev/peps/pep-0257/#one-line-docstrings
# CapWords class names http://www.python.org/dev/peps/pep-0008/#id31
def function_1(self, param1, param2, param3, param4, param5, param6, # underscore function names http://www.python.org/dev/peps/pep-0008/#function-names
param7, param8=None): # restart new line one space right on the pos below the open bracket http://www.python.org/dev/peps/pep-0008/#indentation
"""Function does blah.""" # docstring appears one line after 'def' http://www.python.org/dev/peps/pep-0008/#documentation-strings
# no spces between optional params and its default http://www.python.org/dev/peps/pep-0008/#pet-peeves
# no spaces before brackets
bar1 = False # initialize ALL vars on top
# one var per line (no chaining) https://github.com/fraserharris/Willet-Referrals/pull/67#r639731
bar2 = False # one space before and after = for assignments http://www.python.org/dev/peps/pep-0008/#other-recommendations
array_var = [] # no variable aligning http://www.python.org/dev/peps/pep-0008/#pet-peeves
bar3 = bool(param1 and param2) # one space before and after operators http://www.python.org/dev/peps/pep-0008/#other-recommendations
# be explicit with bool() http://www.python.org/dev/peps/pep-0008/#other-recommendations
call_function(keyword1='Hello', keyword1='World') # no spaces before and after kwargs' equal signs; one space after comma http://www.python.org/dev/peps/pep-0008/#other-recommendations
# almost always should you call functions using kwargs "Explicit is better than implicit." IFF it is simpler "Simple is better than complex."
# blank lines can be used only sparingly http://www.python.org/dev/peps/pep-0008/#blank-lines
call_function() # this is a comment # 2 spaces before inline comments; 1 space after # http://www.python.org/dev/peps/pep-0008/#inline-comments
# no space before function call()
my_dict = { # indent lists, dicts, and tuples like so. http://stackoverflow.com/a/6388237
'a': [b, c], # no spaces before colon; one space after http://www.python.org/dev/peps/pep-0008/#pet-peeves
'd': (e, f) # always use 4 spaces; no tabs http://www.python.org/dev/peps/pep-0008/#tabs-or-spaces
} # never write multi-statement lines by never using the semicolon http://www.python.org/dev/peps/pep-0008/#pet-peeves
if True: # Don't compare boolean values to True or False using == http://www.python.org/dev/peps/pep-0008/#programming-recommendations
if not []: # compare empty sequences without len() http://www.python.org/dev/peps/pep-0008/#programming-recommendations
my_dict['g'] = h # no space before [ http://www.python.org/dev/peps/pep-0008/#pet-peeves
# "Flat is better than nested." http://www.python.org/dev/peps/pep-0020/
try:
blah()
except NamedError, e: # reference e only if you use it https://github.com/fraserharris/Willet-Referrals/pull/67/files?_nid=39415906#r637032
# suffix error classes with 'Error' http://www.python.org/dev/peps/pep-0008/#exception-names
logging.error("oh no: %s" % e) # "Errors should never pass silently. Unless explicitly silenced." http://www.python.org/dev/peps/pep-0020/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment