Skip to content

Instantly share code, notes, and snippets.

@limdauto
Last active September 1, 2016 00:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save limdauto/5860025 to your computer and use it in GitHub Desktop.
Save limdauto/5860025 to your computer and use it in GitHub Desktop.
Lim's implementation of Google Python Style Guide

1. Automated style checking

Google recommends pychecker but either pylint or pyflakes will do as well. See this question on stackoverflow for a discussion of their advantages and disadvantages.

2. Indentation

4 spaces. No tab. 80-column wide. Period.

3. Blank lines

2 blank lines after top-level

def fnc1:
    pass
    
    
def fnc2:
    pass

1 blank line between class definition and first method or between class methods

class C:

    function __init__(self, *args):
        pass

Wing it otherwise with a bit of care.

4. Whitespaces

Only non-intuitive rule is no whitespace around '=' in default arguments.

def complex(real, imag=0.0): return magic(r=real, i=imag)

5. Shebang line

In executable files, not necessary in any other place.

#!/usr/bin/python 

6. Comments

Function's docstring

def func(arg1, arg2, arg3=None):
    """Intro line

    Rant on here. Make sure it's at least 3-line long (j/k)

    Args:
        arg1: An argument
        arg2: Another argument
        arg3: optional argument

    Returns:
        Describe returned value(s) here

    Raises:
        Error
    """
    pass

Class' docstring

class SampleClass(object):
    """Summary of class here.

    Longer class information....
    Longer class information....

    Attributes:
        likes_spam: A boolean indicating if we like SPAM or not.
        eggs: An integer count of the eggs we have laid.
    """

    def __init__(self, likes_spam=False):
        """Inits SampleClass with blah."""
        self.likes_spam = likes_spam
        self.eggs = 0

    def public_method(self):
        """Performs operation blah."""

7. Names:

module_name, package_name, ClassName, method_name, ExceptionName, function_name, GLOBAL_CONSTANT_NAME, global_var_name, instance_var_name, function_parameter_name, local_var_name.

8. Declarations:

  • Classes: for compatibility with Python3 & some other profits, always extend object if the class doesn't explicitly extend any base class.
class SampleClass(object):
    pass


class OuterClass(object):

    class InnerClass(object):
        pass


class ChildClass(ParentClass):
"""Explicitly inherits from another class already."""

Reference: http://google-styleguide.googlecode.com/svn/trunk/pyguide.html

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