I’m just reading the book Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin (see the reviews here). The code examples are in Java but the tips are general and thus they can be applied to Python too. If you want to learn how to write nice code, how to do refactoring, read this book.
“Pylint analyzes Python source code looking for bugs and signs of poor quality. Pylint is a Python tool that checks if a module satisfies a coding standard. Pylint is similar to PyChecker but offers more features, like checking line-code’s length, checking if variable names are well-formed according to your coding standard, or checking if declared interfaces are truly implemented, and much more (see the complete check list). …
Pylint is shipped with Pyreverse which creates UML diagrams for python code.” (source)
With the help of Pylint, you can refactor your code so that it better satisfies coding standards. Its usage is dead simple:
For more details, see the official tutorial.
sudo pip install pylint
I’m using the Eric IDE for Python. I was very happy when I discovered that it has a PyLint plugin! You can install the plugin from Eric.
- lint (lint is the original static code analyzer for C)
If you want to see the errors only, call pylint like this:
pylint -E hello.py
“Pylint is a lint-like tool for Python code. It performs almost all the verifications that pychecker does, and additionally can perform some stylistic verification and coding standard enforcements. The checked code is assigned a mark based on the number and the severity of the encountered problems. The previous mark of a given piece of code is cached so that you can see if the code quality has improved since the last check.”
Pylint is a very nice code checker. If you use Ubuntu, you can install it from the repositories (
sudo apt-get install pylint). Its usage is very simple:
The output is a nice report with suggestions how to improve the code quality. I especially like the “unused import” warnings.
Check out this tiny tutorial for some examples.