如何抑制“未使用的变量” Eclipse / PyDev中的警告 [英] How to suppress "unused variable" warnings in Eclipse/PyDev

查看:750
本文介绍了如何抑制“未使用的变量” Eclipse / PyDev中的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Eclipse / PyDev中禁止未使用的变量警告



当我使用返回元组的函数时,我经常只需要一个值,但是仍然要分配给多个变量。我想暂时把这个警告关掉,所以我可以在更严重的问题上归零。然后,我可以在进行更多的最后检查时重新开始。



如果你想知道为什么我会故意这样做,这只是为了可读性。说一个函数返回一个元组元组,我的代码的几个部分可能与这样的第三个值一起工作:

 标签,内容= myfunc()[2] 

有时候,我可能只对内容部分感兴趣,但我发现这个...

  tmp,content = myfunc()[2] 
content = myfunc()[2] [1]

如果有一个更好的方法,只需不分配给一次性未使用的变量即可,请随时提供答案。

 >>> myfunc()[2] 
('lab','val')
>>> ,v = myfunc()[2]
SyntaxError:invalid syntax
>>> tmp,v = myfunc()[2]
>>>


解决方案

如果不需要变量的值,将它分配给特殊变量 _



就Python而言,实际上没有什么特别的 _ ;但是,对于大多数lint的工具(希望包括PyDev),更重要的是,人类读者 - 它就是另外一个合法的标识符名称。



具有我不需要这个变量,我只是把东西放在这里,因为API /语法/需要它的特殊含义。这意味着他们不会警告你不要使用它。



所以:

  _,content = myfunc()[2] 

是的,你是对的这通常比 myfunc()[2] [1] 更易读。不仅如此,如果 myfunc()[2] 不完全是两个成员,那么这个元组赋值将会抛出, [1] 不会。



很少,这不是一个好主意,因为这个值是你想尽快垃圾回收的东西,并将其绑定到 _ 而不是根本不绑定(例如,通过 [2] [1] )延迟。



更重要的是,这与另一种习惯相冲突,这也使得特殊使用 _ :使用 gettext 用于国际化通常是:

  import gettext 
_ = gettext.gettext

或者,等效:

  from gettext import gettext as _ 

显然你不能使用 _ 作为gettext快捷方式和无意义的标识符。 (你可以/ />实际上可以使用它,因为 gettext 意思是在模块全局级别绑定,而无意义的标识符只能在功能体...但是,这是一个非常糟糕的主意,因为在某些时候你会最终使用 gettext _ )。无论如何,您都不会强迫您使用 _ ,但如果您使用其他任何内容可能会混淆读者(可能是同样的睫毛工具,你正在寻求安抚首先)。所以,你必须决定在任何给定的项目中哪一个更重要。 (通常,如果您使用 gettext ,那将是更重要的一个。)



如果您会反复调用 myfunc 并处理一些值,您可能需要考虑编写一个包装函数:

  def mywrapperfunc():
_,content = myfunc()[2]
返回内容

然后你的代码可以做:

  content = mywrapperfunc )

这有很多优点:




  • 显然比任何需要记住你想要的元组的后半部分更容易阅读,这个元组的索引2是由 myfunc

  • 它给你一个地方放一个漂亮的名字(希望比 mywrapperfunc 更好)和/或评论/ docstrings,以防它不是微不足道的。

  • 这意味着如果你以后更改 myfunc 所以你想要的值现在在索引3而不是2,而第三个元素元组而不是一个2元素元组,你只需要更改 mywrapperfunc 而不是20个不同的代码行。

  • 这也意味着如果你以后想使用冲突的 _ 成语(例如,要使用 gettext 的i18代码,您只需要在一个地方更改。



一方注意:在交互式解释器中, _ 具有特殊含义:它绑定到最后一个交互式命令的结果。但这并不意味着您不能在交互式解释器中使用 _ 。 (事实上​​,它更好,因为任何你藏在那里立即被覆盖,所以非常罕见的GC问题不会出现。)


How to suppress "unused variable" warnings in Eclipse/PyDev

When I'm working with functions that return tuples, I often only need one of the values, but still want to assign to multiple variables. I would like to be able to temporarily turn this warning off so I can zero in on more serious issues. Then, I can turn it back on when doing more of a final check.

If you're wondering why I would do this deliberately, it's just for readability. Say a function returns a tuple of tuples, several parts of my code might work with the third value like this:

label, content = myfunc()[2]

At times, I may only be interested in the 'content' piece, but I find this...

tmp, content = myfunc()[2]

...to be more parallel (and thus more readable) than this:

content = myfunc()[2][1]

If there's a better way to do this simply without assigning to a disposable unused variable, feel free to provide that as an answer.

>>> myfunc()[2]
('lab', 'val')
>>> , v = myfunc()[2]
SyntaxError: invalid syntax
>>> tmp, v = myfunc()[2]
>>> 

解决方案

If you don't need the value of a variable, assign it to the special variable _.

As far as Python is concerned, there is actually nothing special about _; it's just another legal identifier name like any other.

However, for most "lint"-style tools (hopefully including PyDev)—and, more importantly, human readers—it has the special meaning that "I don't need this variable, I'm only putting something here because the API/syntax/whatever requires it". Which means they won't warn you for not using it.

So:

_, content = myfunc()[2]

And yes, you are right that this is often more readable than myfunc()[2][1]. Not only that, but it helps you catch a few more errors—if myfunc()[2] doesn't have exactly two members, the tuple assignment will throw, but the [1] won't.

Very, very rarely, this is not a good idea because the value is something that you want to be garbage collected as soon as possible, and binding it to _ instead of just not binding it at all (e.g., via [2][1]) delays that.

More seriously, this does conflict with a different idiom that also makes special use of _: Code that uses gettext for internationalization typically does:

import gettext
_ = gettext.gettext

Or, equivalently:

from gettext import gettext as _

Obviously you can't use _ as both the gettext shortcut and the meaningless identifier. (You could actually get away with it, because the gettext meaning is bound at module-global level, and the meaningless identifier should only be used inside function bodies… but still, it's a very bad idea to try, because at some point you will end up using the gettext _ in a function after you've assigned a local value that shadows it.) Nothing's forcing you to use _ in either case—but if you use anything else, you are likely to confuse readers (and possibly the same linting tool you're looking to pacify in the first place). So, you have to decide which one is more important to you in any given project. (And usually, if you're using gettext, that's going to be the more important one.)

If you're repeatedly calling myfunc and disposing of some of the values, you might want to consider writing a wrapper function:

def mywrapperfunc():
    _, content = myfunc()[2]
    return content

Then your code can just do:

content = mywrapperfunc()

This has a number of advantages:

  • It's obviously easier to read than anything that requires you to remember that you want the second half of a tuple which is in index 2 of the sequence that's returned by myfunc.
  • It gives you a place to put a nice name (hopefully nicer than mywrapperfunc) and/or comments/docstrings, in case it isn't trivial.
  • It means that if you later change myfunc so the value you want is now in index 3 instead of 2, and the second member of a 3-element tuple instead of a 2-element tuple, you only need to change mywrapperfunc instead of 20 different lines of code.
  • It also means that if you later want to use a conflicting _ idiom (e.g., to i18n your code with gettext), you only need to change it in one place.

One side note: In the interactive interpreter, _ does have a special meaning: it's bound to the result of the last interactive command. But that doesn't mean you can't use _ in the interactive interpreter. (In fact, it's even better there, because anything you stash there is immediately overwritten, so the very rare GC problem doesn't come up.)

这篇关于如何抑制“未使用的变量” Eclipse / PyDev中的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆