This link has been bookmarked by 457 people . It was first bookmarked on 09 Aug 2006, by tim A.
-
15 May 12
-
13 May 12
-
05 May 12
-
14 Apr 12
-
discouraged
-
insistent
-
controversy
-
mangled
-
beware
-
-
12 Apr 12
-
02 Apr 12
-
31 Mar 12
-
22 Mar 12
José Fernando CalcerradaPEP 8 -- Style Guide for Python Code
-
12 Mar 12
-
20 Feb 12
-
07 Feb 12
-
26 Jan 12
-
16 Jan 12
-
Package and Module Names
-
Modules should have short, all-lowercase names
-
Python packages should also have short, all-lowercase names
-
Since module names are mapped to file names, and some file systems are case insensitive and truncate long names
-
Class Names
-
Almost without exception, class names use the CapWords
-
Exception Names
-
suffix "Error"
-
-
14 Jan 12
Nicolas PalopoliThis document gives coding conventions for the Python code comprising the standard library in the main Python distribution. Please see the companion informational PEP describing style guidelines for the C code in the C implementation of Python.
This doc -
12 Jan 12
-
10 Jan 12
Matt Ritterly are strongly recommended over tabs. Most
editors have features that make this easy to do.
Maximum Line Length
Limit all lines to a maximum -
04 Jan 12
-
02 Jan 12
-
11 Dec 11
-
06 Dec 11
-
05 Dec 11
-
CapWords
-
CapWords
-
CapWords
-
CapWords
-
Optional: # Extra indentation is not necessary. foo = long_function_name( var_one, var_two, var_three, var_four)
-
spaces only
-
Imports should be grouped in the following order: 1. standard library imports 2. related third party imports 3. local application/library specific imports You should put a blank line between each group of imports.
-
Package and Module Names Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.
-
some file systems are case insensitive and truncate long names, it is important that module names be chosen to be fairly short
-
Class Names Almost without exception, class names use the CapWords convention.
-
CapWords
-
lowercase, with words separated by underscores
-
Thus "print_" is better than "prnt".
-
Method Names and Instance Variables
-
lowercase with words separated by underscores
-
MAX_OVERFLOW
-
-
26 Nov 11
-
25 Nov 11
-
11 Nov 11
-
06 Nov 11
-
03 Nov 11
-
02 Nov 11
-
22 Oct 11
-
Guido van Rossum
-
This document was adapted from Guido's original Python Style Guide essay[2], with some additions from Barry's style guide[5].
-
code is read much more often than it is written.
-
# Aligned with opening delimiter foo = long_function_name(var_one, var_two, var_three, var_four)
-
# More indentation included to distinguish this from the rest. def long_function_name( var_one, var_two, var_three, var_four): print(var_one)
-
Limit all lines to a maximum of 79 characters.
-
b (single lowercase letter)
-
B (single uppercase letter)
-
lowercase
-
lower_case_with_underscores
-
UPPERCASE
-
UPPER_CASE_WITH_UNDERSCORES
-
CapitalizedWords
-
When using abbreviations in CapWords, capitalize all the letters of the abbreviation
-
mixedCase
-
Capitalized_Words_With_Underscores (ugly!)
-
using a short unique prefix to group related names together
-
The X11 library uses a leading X for all its public functions. In Python, this style is generally deemed unnecessary
-
_single_leading_underscore
-
weak "internal use" indicator
-
single_trailing_underscore_
-
by convention to avoid conflicts with Python keyword
-
__double_leading_underscore
-
when naming a class attribute, invokes name mangling
-
__double_leading_and_trailing_underscore__
-
Never use the characters `l' (lowercase letter el), `O' (uppercase letter oh), or `I' (uppercase letter eye) as single character variable names.
-
Modules should have short, all-lowercase names.
-
Underscores can be used in the module name if it improves readability.
-
Python packages should also have short, all-lowercase names
-
When an extension module written in C or C++ has an accompanying Python module that provides a higher level (e.g. more object oriented) interface, the C/C++ module has a leading underscore (e.g. _socket)
-
Almost without exception, class names use the CapWords convention. Classes for internal use have a leading underscore in addition.
-
Modules that are designed for use via "from M import *" should use the __all__ mechanism to prevent exporting globals
-
the older convention
-
prefixing such globals with an underscore
-
Function names should be lowercase, with words separated by underscores
-
mixedCase is allowed only in contexts where that's already the prevailing style
-
'self' for the first argument to instance methods
-
'cls' for the first argument to class methods
-
To avoid name clashes with subclasses, use two leading underscores to invoke Python's name mangling rules.
-
double leading underscores should be used only to avoid name conflicts with attributes in classes designed to be subclassed.
-
Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL.
-
-
16 Oct 11
-
06 Oct 11
-
29 Sep 11
-
27 Sep 11
-
19 Sep 11
jordan goss" This document gives coding conventions for the Python code comprising the standard library in the main Python distribution."
-
06 Sep 11
-
05 Sep 11
-
Use one leading underscore only for non-public methods and instance variables.
-
-
01 Sep 11
-
23 Aug 11
-
03 Aug 11
-
30 Jul 11
-
07 Jun 11
-
27 May 11
-
19 May 11
-
16 May 11
-
24 Apr 11
-
22 Apr 11
-
18 Apr 11
-
13 Apr 11
-
12 Apr 11
Joseph MorninThis document gives coding conventions for the Python code comprising the standard library in the main Python distribution. Please see the companion informational PEP describing style guidelines for the C code in the C implementation of Python[1]. This document was adapted from Guido's original Python Style Guide essay[2], with some additions from Barry's style guide[5]. Where there's conflict, Guido's style rules for the purposes of this PEP. This PEP may still be incomplete (in fact, it may never be finished <wink>).
-
-
Note that most importantly, the """ that ends a multiline docstring should be on a line by itself, and preferably preceded by a blank line, e.g.: """Return a foobang Optional plotz says to frobnicate the bizbaz first. """
-
If you have to have Subversion, CVS, or RCS crud in your source file, do it as follows. __version__ = "$Revision: 64bda015861d $" # $Source$ These lines should be included after the module's docstring, before any other code, separated by a blank line above and b
-
Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.
-
Since module names are mapped to file names, and some file systems are case insensitive and truncate long names, it is important that module names be chosen to be fairly short -- this won't be a problem on Unix, but it may be a problem when the code is transported to older Mac or Windows versions, or DOS.
-
When an extension module written in C or C++ has an accompanying Python module that provides a higher level (e.g. more object oriented) interface, the C/C++ module has a leading underscore (e.g. _socket).
-
Almost without exception, class names use the CapWords convention. Classes for internal use have a leading underscore in addition.
-
Modules that are designed for use via "from M import *" should use the __all__ mechanism to prevent exporting globals, or use the older convention of prefixing such globals with an underscore (which you might want to do to indicate these globals are "module non-public").
-
Function names should be lowercase, with words separated by underscores as necessary to improve readability. mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.
-
Use one leading underscore only for non-public methods and instance variables. To avoid name clashes with subclasses, use two leading underscores to invoke Python's name mangling rules.
-
Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL.
-
-
28 Mar 11
-
21 Mar 11
-
20 Mar 11
-
16 Mar 11
-
23 Feb 11
-
16 Feb 11
-
09 Feb 11
-
08 Feb 11
-
One of Guido's key insights is that code is read much more often than it is written. The guidelines provided here are intended to improve the readability of code
-
Two good reasons to break a particular rule: (1) When applying the rule would make the code less readable, even for someone who is used to reading code that follows the rules. (2) To be consistent with surrounding code that also breaks it (maybe for historic reasons)
-
invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
-
Limit all lines to a maximum of 79 characters.
-
For flowing long blocks of text (docstrings or comments), limiting the length to 72 characters is recommended.
-
Long lines can be broken over multiple lines by wrapping expressions in parentheses.
-
Separate top-level function and class definitions with two blank lines. Method definitions inside a class are separated by a single blank line.
-
For Python 3.0 and beyond, UTF-8 is preferred over Latin-1, see PEP 3120.
-
HOULD use English words wherever feasible (in many cases, abbreviations and technical terms are used which aren't English)
-
Imports should usually be on separate lines, e.g.: Yes: import os import sys
-
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
-
1. standard library imports 2. related third party imports 3. local application/library specific imports
-
You should put a blank line between each group of imports.
-
Always use the absolute package path for all imports.
-
-
03 Feb 11
-
25 Jan 11
-
17 Jan 11
-
26 Dec 10
-
17 Dec 10
-
13 Dec 10
-
12 Dec 10
-
08 Dec 10
-
Hobgoblin
-
across the wide spectrum of
-
Readability counts
-
-
02 Dec 10
-
25 Nov 10
-
19 Nov 10
-
04 Nov 10
-
31 Oct 10
-
25 Oct 10
-
Indentation
-
Use 4 spaces per indentation level
-
Never mix tabs and spaces.
-
second-most popular way is with tabs only.
-
-
22 Oct 10
-
16 Oct 10
-
12 Oct 10
-
11 Oct 10
-
30 Sep 10
-
_single_leading_underscore: weak "internal use" indicator. E.g. "from M import *" does not import objects whose name starts with an underscore. - single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g. Tkinter.Toplevel(master, class_='ClassName') - __double_leading_underscore: when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo; see below). - __double_leading_and_trailing_underscore__: "magic" objects or attributes that live in user-controlled namespaces. E.g. __init__, __import__ or __file__. Never invent such names; only use them as documented.
-
Class Names Almost without exception, class names use the CapWords convention. Classes for internal use have a leading underscore in addition.
-
Function Names Function names should be lowercase, with words separated by underscores as necessary to improve readability. mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.
-
Function and method arguments Always use 'self' for the first argument to instance methods. Always use 'cls' for the first argument to class methods. If a function argument's name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus "print_" is better than "prnt". (Perhaps better is to avoid such clashes by using a synonym.)
-
Method Names and Instance Variables Use the function naming rules: lowercase with words separated by underscores as necessary to improve readability. Use one leading underscore only for non-public methods and instance variables. To avoid name clashes with subclasses, use two leading underscores to invoke Python's name mangling rules. Python mangles these names with the class name: if class Foo has an attribute named __a, it cannot be accessed by Foo.__a. (An insistent user could still gain access by calling Foo._Foo__a.) Generally, double leading underscores should be used only to avoid name conflicts with attributes in classes designed to be subclassed. Note: there is some controversy about the use of __names (see below).
-
-
24 Sep 10
-
23 Sep 10
-
21 Sep 10
-
07 Sep 10
-
06 Sep 10
-
23 Aug 10
-
04 Aug 10
-
16 Jul 10
-
12 Jul 10
-
The preferred place to break around a binary operator is *after* the operator, not before it.
-
-
09 Jul 10
-
26 Jun 10
-
06 Jun 10
-
__double_leading_and_trailing_underscore__
-
"magic" objects or attributes that live in user-controlled namespaces.
-
__init__, __import__ or __file__
-
Almost without exception, class names use the CapWords convention.
-
-
03 Jun 10
-
One of Guido's key insights is that code is read much more often than it is written.
-
Never mix tabs and spaces.
-
Limit all lines to a maximum of 79 characters.
-
Always use the absolute package path for all imports.
-
Don't use spaces around the '=' sign when used to indicate a keyword argument or a default parameter value.
-
-
28 May 10
-
Use 4 spaces per indentation level.
-
ever mix tabs and spaces.
-
Limit all lines to a maximum of 79 characters.
-
Separate top-level function and class definitions with two blank lines.
-
Method definitions inside a class are separated by a single blank line.
-
Use blank lines in functions, sparingly, to indicate logical sections.
-
-
25 May 10
-
Use 4 spaces per indentation level.
-
The most popular way of indenting Python is with spaces only.
-
Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
-
For new projects, spaces-only are strongly recommended over tabs.
-
Limit all lines to a maximum of 79 characters.
-
For flowing long blocks of text (docstrings or comments), limiting the length to 72 characters is recommended.
-
Separate top-level function and class definitions with two blank lines.
-
Method definitions inside a class are separated by a single blank line.
-
All identifiers in the Python standard library MUST use ASCII-only identifiers, and SHOULD use English words wherever feasible (in many cases, abbreviations and technical terms are used which aren't English).
-
Imports should usually be on separate lines
-
it's okay to say this though: from subprocess import Popen, PIPE
-
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
-
Imports should be grouped in the following order
-
a blank line between each group of imports
-
Conventions for writing good documentation strings (a.k.a. "docstrings") are immortalized in PEP 257 [3].
-
Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.
-
Almost without exception, class names use the CapWords convention.
-
Function names should be lowercase, with words separated by underscores as necessary to improve readability.
-
Always use 'self' for the first argument to instance methods. Always use 'cls' for the first argument to class methods.
-
Method Names and Instance Variables Use the function naming rules
-
Constants are usually declared on a module level and written in all capital letters with underscores separating words.
-
If your public attribute name collides with a reserved keyword, append a single trailing underscore to your attribute name. This is preferable to an abbreviation or corrupted spelling.
-
For simple public data attributes, it is best to expose just the attribute name, without complicated accessor/mutator methods.
-
use properties to hide functional implementation behind simple data attribute access syntax
-
String exceptions in new code are forbidden, because this language feature is being removed in Python 2.6
-
Modules or packages should define their own domain-specific base exception class, which should be subclassed from the built-in Exception class
-
When raising an exception, use "raise ValueError('message')" instead of the older form "raise ValueError, 'message'".
-
When catching exceptions, mention specific exceptions whenever possible instead of using a bare 'except:' clause.
-
Additionally, for all try/except clauses, limit the 'try' clause to the absolute minimum amount of code necessary.
-
-
23 May 10
-
16 May 10
-
15 May 10
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.