Florent Xicluna
EuroPython 2013 - Tuesday, July 2nd
“One of Guido's key insights is that code is read much more often than it is written."
“Consistency with PEP 8 style guide is important. Consistency within a project is more important. Consistency within one module or function is most important.”
TODO: define a style-guide for your project, based on PEP 8 hopefully.
“… is the Hobgoblin of Little Minds.”
Some projects choose to adapt PEP8 rules for valid reasons.
Example:
Twisted does follow much of PEP 8 (as is documented in the Twisted coding standard. However, Twisted prefers camelCase for variables, functions, and methods (whereas PEP 8 recommends underscores) because Twisted predates PEP 8 and converting the entire codebase now is infeasible.
(from Twisted FAQ)
This document gives coding conventions for the C code comprising the C implementation of Python. Please see the companion informational PEP describing style guidelines for Python code.
Note, rules are there to be broken. Two good reasons to break a particular rule...
This PEP documents the semantics and conventions associated with Python docstrings.
The aim of this PEP is to standardize the high-level structure of docstrings: what they should contain, and how to say it.
Limit all lines to a maximum of 79 characters.
There are still many devices around that are limited to 80 character lines; plus, limiting windows to 80 characters makes it possible to have several windows side-by-side.
Therefore, please 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.
Blank lines:
Few reasons to limit line length:
However make it fit your own needs. Example:
Example:
hg diff | pep8 --diff
git diff | pep8 --diff
pep8 --max-line-length 99
Usage: pep8.py [options] input ...
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose print status messages, or debug with -vv
-q, --quiet report only file names, or nothing with -qq
--first show first occurrence of each error
--select=errors select errors and warnings (e.g. E,W6)
--ignore=errors skip errors and warnings (e.g. E4,W)
--show-source show source code for each error
--show-pep8 show text of PEP 8 for each error (implies --first)
--statistics count errors and warnings
--max-line-length=n set maximum allowed line length (default: 79)
--format=format set the error format [default|pylint|<custom>]
--diff report only lines changed according to the unified
diff received on STDIN
Configuration:
The project options are read from the [pep8] section of the tox.ini
file or the setup.cfg file located in any parent folder of the path(s)
being processed. Allowed options are: exclude, filename, select,
ignore, max-line-length, hang-closing, count, format, quiet, show-
pep8, show-source, statistics, verbose.
--config=path user config file location (default:
/Users/user/.config/pep8)
Don't:
x = 1
y = 2
long_variable = 3
Do:
x = 1
y = 2
long_variable = 3
Don't:
# Arguments on first line forbidden when not using vertical alignment
foo = long_function_name(var_one, var_two,
var_three, var_four)
Do:
# Aligned with opening delimiter
foo = long_function_name(var_one, var_two,
var_three, var_four)
Don't:
# Further indentation required as indentation is not distinguishable
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
Do:
# More indentation included to distinguish this from the rest.
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
Do:
my_list = [
(1, 2, 3),
(4, 5, 6),
]
result = some_function_that_takes_arguments(
'abc',
'def',
)
Do (also):
my_list = [
(1, 2, 3),
(4, 5, 6),
]
result = some_function_that_takes_arguments(
'abc',
'def',
)
PyFlakes checks additional errors:
module
imported but unusedmodule
from line N
shadowed by loop variablemodule
import *' used; unable to detect undefined namesF404 - future import(s) name
after other statements
F811 - redefinition of unused name
from line N
name
from line N
name
name
in __all__
name
... referenced before assignmentThe Flake8 tool is a wrapper tool around:
Main features:
pep8.py
Configuration:
The project options are read from the [flake8] section of the tox.ini
file or the setup.cfg file located in any parent folder of the path(s)
being processed. Allowed options are: exclude, filename, select,
ignore, max-line-length, hang-closing, count, format, quiet, show-
pep8, show-source, statistics, verbose, builtins, max-complexity.
--config=path user config file location (default:
/Users/user/.config/flake8)
Configuration (tox.ini or setup.cfg in your project folder):
[flake8]
ignore = E226,E302,E41
max-line-length = 160
Compare to pep8.py standalone:
[pep8]
ignore = E226,E302,E41
max-line-length = 160
Your favorite editor/IDE has support for pep8.py/flake8 of course.
Concerned about code-quality tools?
Subscribe to the mailing-list: code-quality@python.org
Documentation:
Twitter: @florentxicluna
Built with Landslide: github.com/adamzap/landslide
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Help | h |