分析Python程式碼的好工具PyLint

jieforest發表於2012-06-15
Python code analysis can be a heavy subject, but it can be very helpful in making your programs better. There are several Python code analyzers that you can use to check your code and see if they conform. to standards.

Pylint is probably the most popular. It’s very configurable, customizable and pluggable too. It also checks your code to see if it conforms to PEP8, the official style. guide of Python Core and it looks for programming errors too. We’re going to spend a few minutes looking at some of the things you can do with this handy tool.


Getting Started

Sadly, pylint isn’t included with Python, so you’ll need to go out and download it from Logilab or PyPI. If you have SetupTools installed, then you can install it using easy_install, like this:

CODE:

easy_install pylintNow you should have pylint installed and ready to roll!

Analyzing Your Code


The latest version as of this writing is 0.25.1. Once pylint is installed, you can run it on the command line without any arguments to see what options it accepts. Now we need some code to test with. Since I wrote some crummy code for my PyChecker article last year, we’ll re-use that here and see if pylint picks up the same problems. There should be four issues. Here’s the code:

CODE:

01.import sys
02.
03.########################################################################
04.class CarClass:
05.""""""
06.
07.#----------------------------------------------------------------------
08.def __init__(self, color, make, model, year):
09."""Constructor"""
10.self.color = color
11.self.make = make
12.self.model = model
13.self.year = year
14.
15.if "Windows" in platform.platform():
16.print "You're using Windows!"
17.
18.self.weight = self.getWeight(1, 2, 3)
19.
20.#----------------------------------------------------------------------
21.def getWeight(this):
22.""""""
23.return "2000 lbs"

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-732928/,如需轉載,請註明出處,否則將追究法律責任。

相關文章