为什么 Pylint 反对单字符变量名? [英] Why does Pylint object to single-character variable names?
问题描述
我仍然习惯于 Python 约定并使用 Pylint 使我的代码更加 Pythonic,但我对 Pylint 不喜欢单字符变量名这一事实感到困惑.我有几个这样的循环:
I'm still getting used to Python conventions and using Pylint to make my code more Pythonic, but I'm puzzled by the fact that Pylint doesn't like single character variable names. I have a few loops like this:
for x in x_values:
my_list.append(x)
当我运行 pylint
时,我得到 无效名称x"对于类型变量(应该匹配 [a-z_][a-z0-9_]{2,30}
- 这表明有效的变量名称必须在 3 到 31 个字符之间,但我已经看过了通过 PEP8 命名约定,我看不到关于单个小写字母的任何明确内容,我确实看到很多使用它们的示例.
and when I run pylint
, I'm getting Invalid name "x" for type variable (should match [a-z_][a-z0-9_]{2,30}
-- that suggests that a valid variable name must be between 3 and 31 characters long, but I've looked through the PEP8 naming conventions and I don't see anything explicit regarding single lower case letters, and I do see a lot of examples that use them.
我在 PEP8 中是否缺少某些东西,或者这是 Pylint 独有的标准吗?
Is there something I'm missing in PEP8 or is this a standard that is unique to Pylint?
推荐答案
Pylint 不仅检查 PEP8 建议.它也有自己的建议,其中之一就是变量名应该是描述性的,不能太短.
Pylint checks not only PEP8 recommendations. It has also its own recommendations, one of which is that a variable name should be descriptive and not too short.
您可以使用它来避免此类短名称:
You can use this to avoid such short names:
my_list.extend(x_values)
或者调整 Pylint 的配置告诉 Pylint 什么变量名好.
Or tweak Pylint's configuration to tell Pylint what variable name are good.
这篇关于为什么 Pylint 反对单字符变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!