Part 1
Code Layout
Line Structure
Indentation
Python uses indentation levels to determine where the next line belongs to. I use Gedit with the Draw Spaces plugin enabled. In Gedit go to Edit > Preferences > Plugins and select the Draw Spaces Plugin.
Do not mix tabs and spaces in Python code. It is best to use spaces only for indentation. I personally use 2 spaces for indentation.
Indentation Example
if foo == 'time': print 'foo' else: print 'not foo' class jt_gui: def __init__(self, inifile): self.foo2 = 'x' def foo3(self): self.foo3 = 'y'
Line Length
Limit line length to 79 characters. Two or more physical lines can be joined into a logical line with explicit line joining or implicit line joining.
A line ending in a backslash cannot carry a comment. A backslash does not continue a comment.
Explicit line joining
if 1900 < year < 2100 and 1 <= month <= 12 \ and 1 <= day <= 31 and 0 <= hour < 24 \ and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date return 1
Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes.
Implicit line joining
month_names = ['Januari', 'Februari', 'Maart', # These are the 'April', 'Mei', 'Juni', # Dutch names 'Juli', 'Augustus', 'September', # for the months 'Oktober', 'November', 'December'] # of the year