6

Perl has the strict and warnings pragmas and a couple of CPAN modules for encouraging good style and reducing errors. Is there a Python counterpart that can help me avoid such problems and bad practices while it encourages me to use a more Pythonic structure?

I should make it clear that I do not know what I want to avoid. There is probably a lot of traps to fall into, and I'm not looking for a substitute to reading good code, reading about common errors and writing a lot, but for a complementing automated resource.

casperOne
  • 73,706
  • 19
  • 184
  • 253
Tim
  • 13,904
  • 10
  • 69
  • 101
  • 3
    `pylint` `pyflakes` `pychecker` – Katriel Jul 08 '11 at 19:19
  • 1
    It's possible to integrate pylint/pyflakes/pychecker into many different editors / IDEs. I personally use pylint integrated with emacs (flymake-mode) which highlights errors and code cleanliness problems (e.g. unused imports or unused function arguments) as they occur. http://stackoverflow.com/questions/1259873/how-can-i-use-emacs-flymake-mode-for-python-with-pyflakes-and-pylint-checking-cod – Wes McKinney Jul 08 '11 at 19:52

4 Answers4

10

I believe pylint (http://www.logilab.org/857) is the most most common tool used for this purpose.

sshannin
  • 2,767
  • 1
  • 22
  • 25
6

The classic style guide for Python is PEP8. If you are interested in style errors, a checker for PEP8 can be found at http://pypi.python.org/pypi/pep8.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
2

googling for Python lint yielded this: http://pychecker.sourceforge.net/

jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
  • 1
    I tried "nagging", "strict", "boilerplate" etc. Never heard "lint" in this context -- thanks! – Tim Jul 08 '11 at 19:16
  • 2
    @Tim: There's some background on the [Wikipedia page for the original `lint`](http://en.wikipedia.org/wiki/Lint_(software)). – hammar Jul 08 '11 at 19:18
1

Don't forget the -t option to the python interpreter for tab warnings and if you're currently using 2.x and want to minimize the trauma of switching to 3.x in the future, the -3 option will help.

John Gaines Jr.
  • 11,174
  • 1
  • 25
  • 25
  • I just got saved by `-3` thanks to you! Thanks :) "SyntaxWarning: tuple parameter unpacking has been removed in 3.x" – Tim Jul 08 '11 at 21:27