Code Style Guidelines
Code style for fonty python follows the guidelines for Python style as outlined in the following Python project documents, unless otherwise noted:
- Guido van Rossum's Python Style Guide
- PEP 8: Style Guide for Python Code
- PEP 257: Docstring Conventions
When in doubt, refer to the above documents.
Fonty Python Specific Guidelines
A summary of important guidelines, as well as deviations from the standard Python ones are below
Indentation
Indents should be 4 spaces per indentation level. Never use tabs.
Line length
Do not allow lines to exceed 79 characters. Use logical breaks where possible, and a backslash break, if necessary.
Whitespace
Expressions and Assignments
- Always surround with a single space on either side:
- assignment (=)
- augmented assignment (+=, -= etc.)
- comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not)
- Booleans (and, or, not)
Yes:
x = foo if x == foo for x in foo while x <= foo and y >= bar
No:
x=foo, x =foo, x= foo if x==foo, if x== foo, if x ==foo
- Separate with a single space after:
- commas (,)
Yes
def foo(bar, baz, x=boom):
No
def foo(bar,baz,x=boom) :
- Do not surround with a single space:
- arithmetic operators (+, -, ) (Discretion may be used for readability)
- keyword arguments or default value assignments (=)
- immediately inside parenthesis, brackets, and braces
- immediately before colons, semi-colons, or commas
- immediately before open parenthesis in function argument lists
Yes
def complex(real, imag=0.0): return magic(r=real, i=imag)
No
def complex ( real , imag = 0.0 ) : return magic ( r = real , i = imag )
Naming
Variables
Classes
Methods
Docstrings
Document all methods and classes with a docstring. Use a multi-line form for simple cases also. This encourages expansion of docstrings as needed.
Yes
def complex(): """ Form a complex number. """ def complex(real=0.0, imag=0.0): """ Form a complex number. Keyword arguments: real -- the real part (default 0.0) imag -- the imaginary part (default 0.0) """ if imag == 0.0 and real == 0.0: return complex_zero ...
