Code style

A place for Agile's Python code style. If you have a stylesheet you want to share, please add it.

First, read the PEP8 Style Guide for Python. — written by its author (discoverer?), Guido van Rossum, and others.

This is also really good.

Pure Python

 * Setting variables should always have space around the equals.
 * However, don't put space around the equals when setting arguments:.
 * Space math like you would normally, e.g. space around the operators (+, -, etc), except.
 * Don't put spaces around parentheses.
 * Avoid the line continuation character; there's usually a better way to break lines.
 * Commas should always have space after them, except when they're inside indices for arrays, like.
 * Always a space after #, and start 'sentence-like' comments with a capital.
 * Instead of saying  just say.
 * and  are not good variable names.

NumPy and SciPy

 * but if you only use a couple of functions, import them directly.
 * Don't bother importing NumPy if you only need &pi; or the exponential e. These are available in the built-in  module — don't force people to have NumPy for this.
 * Only force floats when you need to, e.g. 1/np.pi will always be a float, so no need for 1.0/np.pi
 * When you need to force floats, it's clearer to write  than   by itself.

IPython Notebooks

 * The goal is to make the text and code work together and read like an essay.
 * Remove as many comments as possible from the code and make them into text blocks.
 * Only instantiate a figure object if you really need one.
 * Always do  or similar, to avoid Matplotlib reporting back on the object.
 * Use namespaces as if Pylab wasn't there, e.g.  instead of just.
 * All the code must work — restart the kernel and check it all runs before calling it 'done'.

Source code in the wiki

 * Use tags for inline code
 * Use for blocks of code (or   if your code contains   (e.g. XML)
 * Use  to specify Python as the language (many languages are supported)
 * Add the argument  to highlight a line of the code (integer or string)
 * For example (from Filtering horizons):

Renders as...