Python's Style Guidelines, Concise Summary |
© 2008, Martin Rinehart |
This page summarizes the Python style guidelines in PEP 8 (authors: Guido van Rossum and Barry Warsaw). As is appropriate for a style guideline, that document includes reasons for its recommendations and examples of good and bad style. This is strictly a summary document. Its target audience is the programmer who has read, but not memorized, PEP 8.
Naming Conventions |
|
||||||||
Note: These are all public. Any of them may be prefixed with '_' for a non-public. Use double-underscore prefix for name mangling in classes designed for subclassing. Use single-underscore suffix to avoid Python reserved words. Use __xxx__ only as documented. Don't use letter 'O' and numeral zero where they could be confused. Ditto for 'i', 'l' and numeral one.
| |||||||||
On Style Guidelines |
|
||||||||
| General Coding |
|
||||||||||||
| Comments and Docstrings |
|
||||||||||||
| Naming | Formatting | Other | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Packages | lower | ||||||||||||
| Modules | lower |
|
Use __all__ mechanism if intended use is from mod import *.
| ||||||||||
| Imports | n/a | Location: just after module docstring [and version crud]. Order: blank line,standard library, blank, third party, blank, local, blank, [ __all__ specs, blank] One module per line. Use absolute (not relative) import specs. |
Import module, not names, to avoid name conflicts. | ||||||||||
| Module Data | globals: lower_and_underscores '_' prefix for non-public |
||||||||||||
| Module "Constants" | |||||||||||||
| Functions | lower_and_underscores '_' prefix for non-public |
Blank lines, sparingly, between sections. | |||||||||||
| Parameters | |||||||||||||
| Other Function Variables | |||||||||||||
| Classes (including Exceptions) | UpperAndLower | Note: no more string exceptions. Classes only. Use suffix 'Error' on exceptions (if they are errors). |
|||||||||||
| Class Methods | cls is first arg. |
||||||||||||
| Class Variables | |||||||||||||
| Class Instances | |||||||||||||
| Class Instance Methods | Use non-publics in preference to publics.
self is first arg.
Expose names, not accessor/mutator functions. Use properties as needed.
Use double leading underscores (name mangling) to avoid name conflicts when subclassing is intended.
| ||||||||||||
| Class Instance Variables | |||||||||||||
| Suites | n/a | Indent four spaces. (Use /t and /tt for old code.) Block comments indented to level of following code. Paragraphs within block comments: a single '#'. |
|||||||||||
| Statements | n/a | Compound statements generally discouraged. Use suites after ':' except for tiny things. raise SomeError('message')except SomeError[, OtherError]: # use specific error(s) |
try: ... finally: better than bare except:.Minimize code after try:.
| ||||||||||
| Expressions | n/a |
|
Use ''.join() to concatenate strings.Comparisons to singletons ( None) should be is [not].Don't use if x when you mean if x is not None.Use string methods, not string functions. Use x.startswith() and x.endswith(), not slicing.(See PEP 8 for strings, Python 2.3 and earlier.) Use isinstance() instead of type comparisons.Don't use trailing whitespace at end of string literals. |
||||||||||