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.
4 spaces. No tab. 80-column wide. Period.
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.
Only non-intuitive rule is no whitespace around '=' in default arguments.
def complex(real, imag=0.0): return magic(r=real, i=imag)
In executable files, not necessary in any other place.
#!/usr/bin/python
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."""
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.
- 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