Python 标准库代码 Pylint 评级较低的原因

发布于 2024-09-12 01:01:54 字数 324 浏览 3 评论 0原文

一位朋友告诉我有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窗影残 2024-09-19 01:01:54

Pylint 的默认设置相当严格,并且会抱怨不应该发生的事情。例如,如果您使用 foo(**kwargs),您会收到一条有关使用“magic”的消息。有时,pylint 似乎是从 Java 程序员的角度来看待 Python。

您必须查看具体消息并决定是否同意它们。

其他问题包括无法执行特定于平台的条件。在 os.py 中,它抱怨:

F:119: Unable to import 'riscos'

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:

F:119: Unable to import 'riscos'
差↓一点笑了 2024-09-19 01:01:54

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文