问题:为什么pylint反对单字符变量名?

我仍然习惯于使用python约定并使用pylint使我的代码更具pythonic风格,但是我对pylint不喜欢单字符变量名感到困惑。我有几个这样的循环:

for x in x_values:
   my_list.append(x)

当我运行时pylint,我得到Invalid name "x" for type variable (should match [a-z_][a-z0-9_]{2,30}-这表明一个有效的变量名长度必须在3到31个字符之间,但是我浏览了PEP8命名约定,但没有看到任何有关单个小写字母的明确信息,我确实看到了很多使用它们的示例。

在PEP8中我缺少什么还是这是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)

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.

Is there something I’m missing in PEP8 or is this a standard that is unique to pylint?


回答 0

PyLint不仅检查PEP8建议。它也有自己的建议,其中之一就是变量名应具有描述性且不能太短。

您可以使用它来避免这样的短名称:

my_list.extend(x_values)

或调整PyLint的配置,以告诉PyLint什么变量名是好的。

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)

Or tweak PyLint’s configuration to tell PyLint what variable name are good.


回答 1

关于格尼alex所指出的内容的更多详细信息:您可以告诉PyLint为变量名(即使您少于三个字符)清楚地将变量名(您发誓发誓)作exceptions。在标题下找到或添加到您的pylintrc文件中[FORMAT]

# Good variable names which should always be accepted, separated by a comma
good-names=i,j,k,ex,Run,_,pk,x,y

这里pk(用于主键),x和y是我添加的变量名。

A little more detail on what gurney alex noted: you can tell PyLint to make exceptions for variable names which (you pinky swear) are perfectly clear even though less than three characters. Find in or add to your pylintrc file, under the [FORMAT] header:

# Good variable names which should always be accepted, separated by a comma
good-names=i,j,k,ex,Run,_,pk,x,y

Here pk (for primary key), x, and y are variable names i’ve added.


回答 2

在强类型语言中,1个字母的名称变量可能没问题,因为通常会在变量的声明中或函数/方法原型中获得名称旁边的类型:

bool check_modality(string a, Mode b, OptionList c) {
    ModalityChecker v = build_checker(a, b);
    return v.check_option(c);
}

在Python中,您不会获得此信息,因此,如果您编写:

def check_modality(a, b, c):
    v = build_checker(a, b)
    return v.check_option(c)

对于该功能可以做什么,如何调用以及返回什么,您绝对不会给维护团队提供任何线索。因此,在Python中,您倾向于使用描述性名称:

def check_modality(name, mode, option_list):
    checker = build_checker(name, mode)
    return checker.check_option(option_list)

您甚至可以添加一个文档字符串来说明这些东西的作用以及期望的类型。

In strongly typed languages, 1 letter name variables can be ok-ish, because you generally get the type next to the name in the declaration of the variable or in the function / method prototype:

bool check_modality(string a, Mode b, OptionList c) {
    ModalityChecker v = build_checker(a, b);
    return v.check_option(c);
}

In Python, you don’t get this information, so if you write:

def check_modality(a, b, c):
    v = build_checker(a, b)
    return v.check_option(c)

you’re leaving absolutely no clue for the maintenance team as to what the function could be doing, and how it is called, and what it returns. So in Python, you tend to use descriptive names:

def check_modality(name, mode, option_list):
    checker = build_checker(name, mode)
    return checker.check_option(option_list)

and you even add a docstring explaining what the stuff does and what types are expected.


回答 3

如今,还有一个选项可以覆盖正则表达式。即,如果您想允许单个字符作为变量:

pylint --variable-rgx="[a-z0-9_]{1,30}$" <filename>

因此,pylint将与PEP8相匹配,并且不会带来其他违规行为。您也可以将其添加到中.pylintrc

Nowadays there is also a option to override regexp. I.e. if you want to allow single characters as variables:

pylint --variable-rgx="[a-z0-9_]{1,30}$" <filename>

So, pylint will match PEP8 and will not bring additional violations on top. Also you can add it to .pylintrc.


回答 4

更深层次的原因是,你可能还记得你的原意abcxy,和z是指当你写你的代码,但是当别人读它,甚至当你回到你的代码,代码变得更加可读当你给它是一个语义名称。我们不会在黑板上写东西然后再擦除它。我们正在编写的代码可能会存在十年或更长时间,并且会被阅读很多次。

使用语义名称。我使用语义的名称一直喜欢ratiodenominatorobj_generatorpath,等这可能需要额外的两秒钟他们打出来,但是你节省试图找出你所写的,甚至一半,然后一小时的时间是值得的。

The deeper reason is that you may remember what you intended a, b, c, x, y, and z to mean when you wrote your code, but when others read it, or even when you come back to your code, the code becomes much more readable when you give it a semantic name. We’re not writing stuff once on a chalkboard and then erasing it. We’re writing code that might stick around for a decade or more, and be read many, many times.

Use semantic names. Semantic names I’ve used have been like ratio, denominator, obj_generator, path, etc. It may take an extra second or two to type them out, but the time you save trying to figure out what you wrote even half an hour from then is well worth it.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。