Re: Standardizing our Python code style
From: Blair Zajac <blair_at_orcaware.com>
Date: Thu, 22 Oct 2009 10:58:01 -0700
Julian Foad wrote:
The largest inconsistency is 2-space versus 4-space indentation, where 4-space
The major ones, in my opinion from PEP-8 are the following:
Indentation
Use 4 spaces per indentation level.
Tabs or Spaces?
Never mix tabs and spaces.
Maximum Line Length
Limit all lines to a maximum of 79 characters.
Imports
- Imports should usually be on separate lines, e.g.:
Yes: import os
Imports should be grouped in the following order:
1. standard library imports
Pet Peeves
Avoid extraneous whitespace in the following situations:
- Immediately inside parentheses, brackets or braces.
Yes: spam(ham[1], {eggs: 2})
- Immediately before a comma, semicolon, or colon:
Yes: if x == 4: print x, y; x, y = y, x
- Immediately before the open parenthesis that starts the argument
Yes: spam(1)
- Immediately before the open parenthesis that starts an indexing or
Yes: dict['key'] = list[index]
- More than one space around an assignment (or other) operator to
Yes:
x = 1
No:
x = 1
Other Recommendations
- Always surround these binary operators with a single space on
- Use spaces around arithmetic operators:
Yes:
i = i + 1
No:
i=i+1
- Don't use spaces around the '=' sign when used to indicate a
Yes:
def complex(real, imag=0.0):
No:
def complex(real, imag = 0.0):
- Object type comparisons should always use isinstance() instead
Yes: if isinstance(obj, int):
No: if type(obj) is type(1):
- For sequences, (strings, lists, tuples), use the fact that empty
Yes: if not seq:
No: if len(seq)
- Don't compare boolean values to True or False using ==
Yes: if greeting:
No: if greeting == True:
Worse: if greeting is True:
- Comparisons to singletons like None should always be done with
Blair
------------------------------------------------------
|
This is an archived mail posted to the Subversion Dev mailing list.
This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.