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/,如需转载,请注明出处,否则将追究法律责任。