Python: PEP 8 Brush Up
Posted in Journal on Wednesday, October 06, 2010 01:09AM
Keeping with my recent optimizations, I did a little brushing up on PEP 8 - Python's Style Guide. Here's a short list of some things I need to pay more attention to:
- Limit all lines to a maximum of 79 characters, 72 for flowing long blocks of text.
- Wrap single spaces around binary and arithmetic operators.
- Don't use spaces around the '=' sign when used to indicate a keyword argument or a default parameter value.
- "from x import *" does not import _single_underscore_names (weak internal).
- single_trailing_underscores_ can be used to avoid conflicts with Python keywords.
- Never use the characters 'l' (lowercase letter el), 'O' (uppercase letter oh), or 'I' (uppercase letter eye) as single character variable names.
- Class names and exceptions use the CapWords naming convention.
- Exceptions that are errors should use the suffix "Error."
- function_names should be lowercase, words separated by underscores as necessary.
- Constants are usually declared on a module level and written in all capital letters with underscores separating words.
- Use ''.startswith() and ''.endswith() to check for prefixes or suffixes.
- Always use isinstance(obj, (int, str, dict, etc...)) instead of comparing types directly.
by virgohippy |


