Skip to content

Instantly share code, notes, and snippets.

@skopp
Created June 15, 2013 22:20
Show Gist options
  • Save skopp/5789799 to your computer and use it in GitHub Desktop.
Save skopp/5789799 to your computer and use it in GitHub Desktop.

# Python [PEP-8]:

Intermezzo: Coding Style

... (f)or Python, PEP 8 has emerged as the style guide that most projects adhere to; it promotes a very readable and eye-pleasing coding style. Every Python developer should read it at some point; here are the most important points extracted for you:

  • Use 4-space indentation, and no tabs.

    4 spaces are a good compromise between small indentation (allows greater nesting depth) and large indentation (easier to read). Tabs introduce confusion, and are best left out.

  • Wrap lines so that they don't exceed 79 characters.

    This helps users with small displays and makes it possible to have several code files side-by-side on larger displays.

  • Use blank lines to separate functions and classes, and larger blocks of code inside functions.

  • When possible, put comments on a line of their own.

  • Use docstrings.

  • Use spaces around operators and after commas, but not directly inside bracketing constructs: a = f(1, 2) + g(3, 4).

  • Name your classes and functions consistently; the convention is to use CamelCase for classes and lower_case_with_underscores for functions and methods. Always use self as the name for the first method argument (see :ref:`tut-firstclasses` for more on classes and methods).

  • Don't use fancy encodings if your code is meant to be used in international environments. Plain ASCII works best in any case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment