Python 为什么 pylint 反对单字符变量名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21833872/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does pylint object to single character variable names?
提问by Amanda
I'm still getting used to python conventions and using pylintto 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:
我仍然习惯于 python 约定并使用pylint使我的代码更加 pythonic,但我对 pylint 不喜欢单字符变量名这一事实感到困惑。我有几个这样的循环:
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 conventionsand I don't see anything explicit regarding single lower case letters, and I do see a lot of examples that use them. 
当我运行时pylint,我得到Invalid name "x" for type variable (should match [a-z_][a-z0-9_]{2,30}- 这表明有效的变量名称长度必须在 3 到 31 个字符之间,但我已经查看了PEP8 命名约定,我没有看到任何关于单个小写字母的明确内容,而且我确实看到了很多使用它们的示例。
Is there something I'm missing in PEP8 or is this a standard that is unique to pylint?
PEP8 中是否有我遗漏的东西,或者这是 pylint 独有的标准吗?
采纳答案by warvariuc
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.
PyLint 不仅检查 PEP8 建议。它也有自己的建议,其中之一是变量名应该是描述性的,而且不能太短。
You can use this to avoid such short names:
您可以使用它来避免这样的短名称:
my_list.extend(x_values)
Or tweak PyLint's configurationto tell PyLint what variable name are good.
或者调整 PyLint 的配置来告诉 PyLint 什么变量名是好的。
回答by Aaron Hall
The deeper reason is that you may remember what you intended a, b, c, x, y, and zto 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.
更深层次的原因是,你可能还记得你的原意a,b,c,x,y,和z是指当你写你的代码,但是当别人读它,甚至当你回到你的代码,代码变得更加可读当你给它是一个语义名称。我们不是在黑板上写过一次东西然后擦掉它。我们正在编写的代码可能会存在十年或更长时间,并且会被阅读很多很多次。
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.
使用语义名称。我使用过的语义名称如ratio、denominator、obj_generator、path等。 输入它们可能需要额外的一两秒钟,但是您节省的时间甚至半小时后试图弄清楚您写的内容是非常值得的.
回答by gurney alex
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:
在强类型语言中,1 个字母的名称变量是可以的,因为您通常会在变量声明或函数/方法原型中获得名称旁边的类型:
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:
在 Python 中,你不会得到这些信息,所以如果你写:
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:
你完全没有给维护团队留下关于函数可以做什么、如何调用以及返回什么的线索。所以在 Python 中,你倾向于使用描述性名称:
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.
你甚至添加了一个文档字符串来解释这些东西的作用和预期的类型。
回答by mlncn
A little more detail on what gurney alex noted: you can tell PyLint to make exceptions for variable nameswhich (you pinky swear) are perfectly clear even though less than three characters.  Find in or add to your pylintrcfile, under the [FORMAT]header:
关于 gurney alex 指出的内容的更多细节:您可以告诉 PyLint 为变量名称设置例外,即使少于三个字符也非常清楚(您小指发誓)。在标题下找到或添加到您的pylintrc文件中[FORMAT]:
# 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.
这里 pk(用于主键)、x 和 y 是我添加的变量名称。
回答by Jimilian
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, pylintwill match PEP8 and will not bring additional violations on top. Also you can add it to .pylintrc.
因此,pylint将匹配 PEP8 并且不会带来额外的违规行为。您也可以将其添加到.pylintrc.

