Notes on the Google Python Style Guide
A few notes I took while reading the Google Python Style Guide.
Style rules
- Do not end lines with semicolons, and do not place two statements on the same line separated by a semicolon.
- Keep lines under 80 characters. If a logical line needs to span multiple physical lines, wrap it in extra parentheses so it joins implicitly.
- Leave two blank lines between top-level definitions (functions or classes) and one blank line between methods.
- 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 documentArgs,Returns, andRaises. Class docstrings should describe the class and its public attributes. - 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. - If a class does not inherit from anything else, explicitly inherit from
object. - Avoid using
+inside loops to build strings. Uselist.appendand join instead. - Tag temporary workarounds with
TODOcomments. - Even scripts should be importable without running their main entry point. Put the main logic inside
main(). - Naming:
module_name,package_name,ClassName,method_name,ExceptionName,function_name,GLOBAL_VAR_NAME,instance_var_name,function_parameter_name,local_var_name.
Reference: