Advertisement
steve-shambles-2109

200-PEP8-Cheatsheet

Nov 9th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.98 KB | None | 0 0
  1. """PEP8-Cheatsheet
  2. Above is this module's docstring summary line.
  3. This file is not runnable code by the way.
  4.  
  5. This is a multi-line docstring. Paragraphs are separated with blank lines.
  6. Lines conform to 79-column limit.
  7.  
  8. Module and packages names should be short, lower_case_with_underscores.
  9. Notice that this in not PEP8-cheatsheet.py
  10.  
  11. See http://www.python.org/dev/peps/pep-0008/ for more PEP-8 details
  12.  
  13. Python code snippets vol 40.
  14. 200-PEP8-Cheatsheet
  15. stevepython.wordpress.com
  16.  
  17. source:
  18. https://gist.github.com/Vaibhavs10/52cd86ab38145e3f273cc3dba58b7968
  19. """
  20.  
  21.  
  22. import os  # STD lib imports first
  23. import sys  # alphabetical
  24.  
  25. import some_third_party_lib  # 3rd party stuff next
  26. import some_third_party_other_lib  # alphabetical
  27.  
  28. import local_stuff  # local stuff last
  29. import more_local_stuff
  30. import dont_import_two, modules_in_one_line  # IMPORTANT!
  31. from pyflakes_cannot_handle import *  # and there are other reasons it should be avoided # noqa
  32. # Using # noqa in the line above avoids flake8 warnings about line length!
  33.  
  34.  
  35. _a_global_var = 2  # so it won't get imported by 'from foo import *'
  36. _b_global_var = 3
  37.  
  38. A_CONSTANT = 'ugh.'
  39.  
  40.  
  41. # 2 empty lines between top-level funcs + classes
  42. def naming_convention():
  43.     """Write docstrings for ALL public classes, funcs and methods.
  44.    Functions use snake_case.
  45.    """
  46.     if x == 4:  # x is blue <== USEFUL 1-liner comment (2 spaces before #)
  47.         x, y = y, x  # inverse x and y <== USELESS COMMENT (1 space after #)
  48.     c = (a + b) * (a - b)  # operator spacing should improve readability.
  49.     dict['key'] = dict[0] = {'x': 2, 'cat': 'not a dog'}
  50.  
  51.  
  52. class NamingConvention(object):
  53.     """First line of a docstring is short and next to the quotes.
  54.    Class and exception names are CapWords.
  55.    Closing quotes are on their own line
  56.    """
  57.  
  58.     a = 2
  59.     b = 4
  60.     _internal_variable = 3
  61.     class_ = 'foo'  # trailing underscore to avoid conflict with builtin
  62.  
  63.     # this will trigger name mangling to further discourage use from outside
  64.     # this is also very useful if you intend your class to be subclassed, and
  65.     # the children might also use the same var name for something else; e.g.
  66.     # for simple variables like 'a' above. Name mangling will ensure that
  67.     # *your* a and the children's a will not collide.
  68.     __internal_var = 4
  69.  
  70.     # NEVER use double leading and trailing underscores for your own names
  71.     __nooooooodontdoit__ = 0
  72.  
  73.     # don't call anything (because some fonts are hard to distiguish):
  74.     l = 1
  75.     O = 2
  76.     I = 3
  77.  
  78.     # some examples of how to wrap code to conform to 79-columns limit:
  79.     def __init__(self, width, height,
  80.                  color='black', emphasis=None, highlight=0):
  81.         if width == 0 and height == 0 and \
  82.            color == 'red' and emphasis == 'strong' or \
  83.            highlight > 100:
  84.             raise ValueError('sorry, you lose')
  85.         if width == 0 and height == 0 and (color == 'red' or
  86.                                            emphasis is None):
  87.             raise ValueError("I don't think so -- values are %s, %s" %
  88.                              (width, height))
  89.         Blob.__init__(self, width, height,
  90.                       color, emphasis, highlight)
  91.  
  92.     # empty lines within method to enhance readability; no set rule
  93.     short_foo_dict = {'loooooooooooooooooooong_element_name': 'cat',
  94.                       'other_element': 'dog'}
  95.  
  96.     long_foo_dict_with_many_elements = {
  97.         'foo': 'cat',
  98.         'bar': 'dog'
  99.     }
  100.  
  101.     # 1 empty line between in-class def'ns
  102.     def foo_method(self, x, y=None):
  103.         """Method and function names are lower_case_with_underscores.
  104.        Always use self as first arg.
  105.        """
  106.         pass
  107.  
  108.     @classmethod
  109.     def bar(cls):
  110.         """Use cls!"""
  111.         pass
  112.  
  113. # a 79-char ruler:
  114. # 34567891123456789212345678931234567894123456789512345678961234567897123456789
  115.  
  116. """
  117. Common naming convention names:
  118. snake_case
  119. MACRO_CASE
  120. camelCase
  121. CapWords
  122. """
  123.  
  124. # Newline at end of file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement