Programming Style (in Python)
Basics
Whitespace
- Indentation what the brain reconizes, use it regardless of language (and braces)
- 4 space indents
- Spaces after commas and around operators (almost always)
Naming
- joined_lower for functions
- StudlyCaps for classes
Long lines
- Lines < 80 characters long
- Use implied line continuation
Flow
- Docstrings = How to use code (PEP 257
- Comments = Why & how code works
General approaches
Style also includes the general approaches for addressing regularly
encountered problems. E.g.:
- Multi-value assignment
- List comprehensions
- e.g., [a ** 2 for a in range(10)]
- e.g., [a * b for a in range(1, 5) for b in range(1, 5) if a == b]
- e.g., [a * b for a in range(1, 5) for b in range(a, 5)]
- and Generator expressions - ``sum(a ** 2 for a in range(100))
- get() method for dictionaries
if x:
not if x == True:
or if len(x) > 0:
enumerate()
- EAFP (easier to ask forgiveness) no LBYL
- assume that types are right and fail gracefully if not instead of checking types first