With leading white space governing the indentation level and so the grouping of statements, Python code already looks relatively clean.
Yet there are additional tools that will help you improve your code.
The pyflakes checker tries very hard to find errors in Python source files. (It does not care about style.)
Personally I do like to relax the PEP 8 maximum line length of 79 characters.
In general I like to try to keep line length below 80 characters. But if
incidentally is somewhat more, It doesn’t bother me. So I generally use
pycodestyle with the argument
Since I save regularly, I get fast feedback on errors and potential issues. This has probably been the most helpful in teaching me good coding habits and solving problems when they occur.
Documentation is almost as important as your code; it is probably the first thing that a user of a module will read.
So I’m in the process of checking all my Python code with pydocstyle.
Since some of the default checks that it runs are contradictory, I’ve created
.pydocstylerc file in my home directory with the following contents:
[pydocstyle] ignore = D203,D212,D406,D407,D408,D409,D413
This ignores the following violations that I don’t agree with:
- 1 blank line required before class docstring. (Contradicts D211).
- Multi-line docstring summary should start at the first line. (Contradicts D213).
- Section name should end with a newline. (Complains about “Returns:” instead of “Returns”).
- All about underlining section names, which I don’t do.
- Missing blank line after last section.
While yapf is a excellent reformatting tool, it has some limitations on
Python 3. It crashes hard on unicode identifiers, because of its internal
It works fine when your code does not contain unicode identifiers, though. I use it with its standard settings.
For comments, please send me an e-mail.
- Python & standard output redirection on ms-windows
- Profiling Python scripts (5): ent_without_numpy
- Profiling Python scripts (4): vecops.indexate
- Profiling Python scripts (3): stl2ps
- Profiling Python scripts (2): stlinfo