如何处理pylint消息:ID:W0612未使用的变量 [英] How to handle the pylint message: ID:W0612 Unused Variable
问题描述
我正在使用pylint将一些代码更新为PEP 8标准。部分代码抛出W0612未使用的变量错误,但这是因为它使用的模块返回(x,y),例如,在这种特殊情况下仅需要x时,便完成了此操作。
I'm updating some code to PEP 8 standard using pylint. Part of the code is throwing the W0612 unused variable error but it's because it's using a module that returns (x,y) for example when only x is needed in this particular case, this is what's done.
(var_1, var_2) = func()
def func():
a="a"
b="b"
return (a,b)
var_1,但从不使用var_2,因此会引发错误。我该如何处理?我在想这个
var_1 is then returned but var_2 is never used and therefore throws the error. How should I handle this? I'm thinking this
var = func()[0]
处理它的最佳方法是什么?
What is the best way to handle it?
推荐答案
我相信 a,dummy = func()
可以解决问题。 Pylint允许(如果我没记错的话)以 _
或 dummy
开头的未使用的变量名,例如 dummy_index
。
I believe that a, dummy = func()
does the trick. Pylint allows (if I recall correctly) unused variables names that start with _
or dummy
, e.g. dummy_index
.
您可以通过传递-dummy-variables-rgx
Pylint的选项。
You can configure this by passing --dummy-variables-rgx
option to Pylint. This specifies the regex that catches dummy variable names.
来自 Pintint 1.6.0文档:
dummy-variables-rgx:
dummy-variables-rgx:
与虚拟变量名称匹配的正则表达式(即预期不使用)。
默认值:(_ + [a-zA-Z0-9] *?$)|虚拟
A regular expression matching the name of dummy variables (i.e. expectedly not used). Default: (_+[a-zA-Z0-9]*?$)|dummy
注意:使用 _
确实会造成混乱(道具:Sven Marnach)。有一个约定使用单下划线作为半私有标识符的前缀,双下划线当然是特殊Python方法的前缀,最重要的是别名别名 gettext()
在需要本地化的程序中起 _()
的作用,如 _( text to translation)
。
Note: Using _
can indeed cause confusion (props: Sven Marnach). There's a convention to use single underscore as prefix for semi-private identifiers, the double underscore is of course the prefix for special Python methods and on top of that there's a convention to aliasgettext()
function as _()
in programs that need localization as in _("text to translate")
.
这篇关于如何处理pylint消息:ID:W0612未使用的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!