You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Creator Guido van Rossum (Dutch Programmer, Netherlands)
Logo, two snakes
PyPI
The Python Package Index is a repository of software for the Python programming language. The Python Package Index (PyPI) hosts thousands of third-party modules for Python.
Code in the core Python distribution should always use UTF-8 (or ASCII in Python 2).
Code lay-out
Indentation
Use 4 spaces per indentation level.
NO
# Arguments on first line forbidden when not using vertical alignment.foo=long_function_name(var_one, var_two,
var_three, var_four)
# Further indentation required as indentation is not distinguishable.deflong_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
YES
# 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.deflong_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
# Hanging indents should add a level.foo=long_function_name(
var_one, var_two,
var_three, var_four)
The closing brace/bracket/parenthesis on multi-line constructs.
Should a line break before or after a binary operator?
NO
# No: operators sit far away from their operandsincome= (gross_wages+taxable_interest+
(dividends-qualified_dividends) -ira_deduction-student_loan_interest)
YES
# Yes: easy to match operators with operandsincome= (gross_wages+taxable_interest+ (dividends-qualified_dividends)
-ira_deduction-student_loan_interest)
Imports
Imports should usually be on separate lines, e.g.:
NO
importsys, os
YES
importosimportsys
It's okay to say this though:
fromsubprocessimportPopen, 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:
Standard library imports
Related third party imports
Local application/library specific imports
You should put a blank line between each group of imports.
Module level dunder names
Module level "dunders" (i.e. names with two leading and two trailing underscores) such as all , author , version , etc. should be placed after the module docstring but before any import statements except from future imports.
"""This is the example module.This module does stuff."""from __future__ importbarry_as_FLUFL__all__= ['a', 'b', 'c']
__version__='0.1'__author__='Cardinal Biggles'importosimportsys
String Quotes
In Python, single-quoted strings and double-quoted strings are the same. Pick a rule and stick to it. When a string contains single or double quote characters, however, use the other one to avoid backslashes in the string. It improves readability.
For triple-quoted strings, always use double quote characters to be consistent with the docstring convention in (PEP 257)[https://www.python.org/dev/peps/pep-0257].
Whitespace in Expressions and Statements
Yes: ifx==4: printx, y; x, y=y, xNo: ifx==4 : printx , y ; x , y=y , x
Functions, Packages and Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability although use of underscores is discouraged.
Class Names
Class names should normally use the CapWords convention.