Code Style Guidelines (Python)
Python 2.5.2 is an older version of the Python programming language, but the same general principles apply for writing clean and readable code.
Indentation
Use 4 spaces per indentation level (do not use tabs).
Continuation lines should align with the opening delimiter.
Use hanging indents when breaking long lines.
Maximum Line Length
Limit lines to a maximum of 79 characters.
Imports
Place import statements at the top of the file, separated into three sections: standard library, third-party, and local.
Import one module per line and avoid using wildcards imports (e.g., `from module import *`).
Whitespace
1. Use whitespace in expressions and statements sparingly.
2. Avoid trailing whitespace at the end of lines.
3. Put one space around assignment and comparison operators.
4. Do not use spaces around the equals sign when used to indicate a keyword argument or default parameter value.
Comments
1. Write comments to explain the intent and rationale behind code.
2. Use inline comments sparingly and avoid stating the obvious.
3. For multi-line comments, use triple quotes.
4. Keep comments up to date with code.
Naming Conventions
1. Use `lowercase_with_underscores` for variable, function and method names.
2. Use `PascalCase` for ICE variables and class names.
3. Use `UPPERCASE_WITH_UNDERSCORES` for constants.
4. Name modules using `lowercase_with_underscores`.
Functions and Methods
1. Keep functions and methods short and focused on a single task.
2. Use `self` as the first parameter for instance methods.
3. Use `cls` as the first parameter for class methods.
Classes
1. Use the new-style class declaration: `class MyClass(Object):`.
2. Keep class definitions simple and avoid using multiple inheritance if possible.
Exceptions
1. Use exceptions for error handling instead of return codes (this goes for error referrals/messages, as well).
2. Use the most specific exception possible when raising errors.
3. Do not use bare `except` clauses; specify the exceptions you expect to catch.
String formatting
1. Use the `%` operator for string formatting.
2. When formatting multiple items, use a tuple: `”Hello %s, your age is %d” % (name, age)`.
Documentation
1. Write docstrings for all public modules, functions, classes and methods.
2. Use triple quotes for docstrings (`“””`).
3. Follow the reStructuredText (reST) format for docstrings.
Last updated