Notes on the Google Python Style Guide

A few notes I took while reading the Google Python Style Guide.

Style rules

  1. Do not end lines with semicolons, and do not place two statements on the same line separated by a semicolon.
  2. Keep lines under 80 characters. If a logical line needs to span multiple physical lines, wrap it in extra parentheses so it joins implicitly.
  3. Leave two blank lines between top-level definitions (functions or classes) and one blank line between methods.
  4. Docstrings—strings used as documentation on packages, modules, classes, or functions—typically use triple double quotes ("""). Start with a summary line, then a blank line, then the detailed description. Module docstrings should include the license boilerplate. Function and method docstrings should document Args, Returns, and Raises. Class docstrings should describe the class and its public attributes.
  5. Comments start with # (even multi-line comments) and sit two spaces away from code. Comment tricky or complex sections, but do not narrate the code; assume the reader understands Python.
  6. If a class does not inherit from anything else, explicitly inherit from object.
  7. Avoid using + inside loops to build strings. Use list.append and join instead.
  8. Tag temporary workarounds with TODO comments.
  9. Even scripts should be importable without running their main entry point. Put the main logic inside main().
  10. Naming: module_name, package_name, ClassName, method_name, ExceptionName, function_name, GLOBAL_VAR_NAME, instance_var_name, function_parameter_name, local_var_name.

Reference:

Google Python Style Guide