Python 标准库代码 Pylint 评级较低的原因
一位朋友告诉我有关 Pylint 的信息,出于好奇,我对一些标准库模块运行了它。令我惊讶的是,收视率很低。以下是一些运行:
os.py
Your code has been rated at 3.55/10
random.py
Your code has been rated at 4.74/10
我在更多模块上运行了它,发现评级约为 6 - 7。
我想知道这背后的原因是什么? Pylint 是否损坏了,或者影响评级的因素比我意识到的还要多?我问这个问题主要是因为我是 Python 新手,并且依赖 Pylint 来帮助我改进我的编码风格:)
A friend told me about Pylint and just out of curiosity, I ran it against some of the standard library modules. To my surprise, the ratings were low. Here are a few runs:
os.py
Your code has been rated at 3.55/10
random.py
Your code has been rated at 4.74/10
I ran it on some more modules and the found the rating to be ~ 6 - 7.
I was wondering the reason behind this? Is Pylint broken or there are more factors to the rating than I am aware of? I am asking this question particularly cause I am new to Python and was depending on Pylint to help me improve my coding style :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Pylint 的默认设置相当严格,并且会抱怨不应该发生的事情。例如,如果您使用
foo(**kwargs)
,您会收到一条有关使用“magic”的消息。有时,pylint 似乎是从 Java 程序员的角度来看待 Python。您必须查看具体消息并决定是否同意它们。
其他问题包括无法执行特定于平台的条件。在 os.py 中,它抱怨:
Pylint's defaults are quite strict, and complain about things they should not. For example, if you use
foo(**kwargs)
, you get a message about using "magic". Sometimes it seems as if pylint is looking at Python from a Java programmer's point of view.You'd have to look at the specific messages and decide if you agree with them.
Other problems include not being able to do platform-specific conditionals. In os.py, it complains:
Pylint 是在 stdlib 之后很久才编写的。例如,stdlib 不遵守严格的命名约定(PEP008 是最近的,wrt python)。获得“良好”pylint 评级的关键因素:
确保您的代码编写风格符合 Pylint 的期望(或调整 Pylint 以匹配您的风格/约定)。这包括函数、变量、类、方法名称、各个位置的空格等。
以尽可能静态且方便的方式编写 Python 代码,并避免动态技巧。
write docstrings
显然,标准库并不是为了优化 Pylint 对模块的评级而编写的。
使用 Pylint 并不一定会改善你的“编码风格”。然而,在许多情况下,它会让你的代码更容易理解,有时是以一些“Pythonicity”为代价的。
Pylint was written long after the stdlib. And the stdlib does not adhere to strict naming conventions for instance (PEP008 is recent, wrt python). Key factors for getting "good" pylint ratings:
make sure your code writing style is conform to what Pylint is expecting (or tune Pylint to match your style / conventions). This includes function, variables, class, method names, spaces at various places, etc.
write Python code in an as static as convenient way, and avoid dynamic tricks.
write docstrings
Obviously, the standard library is not written to optimize Pylint's ratings of the modules.
Using Pylint will not necessarily improve your "coding style". It will however in a number of cases make your code more easy to understand, sometimes at the cost of some "pythonicity".