python 在python中,是否有一个变量赋值的“pass”等价物
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1419077/
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
In python, is there a "pass" equivalent for a variable assignment
提问by Krystian Cybulski
I am using a library function called get_count_and_price which returns a 2-tuple (count,price). In many places I use both time and price. However, in some I only need time or price. So right now, if I only need count, I assign to (count,price) and leave the price unused.
我正在使用一个名为 get_count_and_price 的库函数,它返回一个 2 元组 (count,price)。在许多地方,我同时使用时间和价格。但是,在某些情况下,我只需要时间或价格。所以现在,如果我只需要计数,我分配给 (count,price) 并保留未使用的价格。
This works great and causes no trouble in and of itself.
这很有效,并且本身不会造成任何麻烦。
However...
然而...
I use Eclipse with PyDev, and the new version 1.5 automatically shows errors and warnings. One of the warnings it shows is unused variables. In the above example, it flags price as unused. This is the sort of behavior which is great and I really appreciate PyDev doing this for me. However, I would like to skip the assignment to price altogether. Ideally, I would like something like:
我将 Eclipse 与 PyDev 一起使用,新版本 1.5 会自动显示错误和警告。它显示的警告之一是未使用的变量。在上面的示例中,它将价格标记为未使用。这是一种很棒的行为,我非常感谢 PyDev 为我做这件事。但是,我想完全跳过对价格的分配。理想情况下,我想要类似的东西:
(count,None) = get_count_and_price()
Now as we all know, None cannot be assigned to. Is there something else I could do in this case?
现在我们都知道, None 不能分配给。在这种情况下,我还能做些什么吗?
I know I could do something like
我知道我可以做类似的事情
count = get_count_and_price()[0]
but I am asking just to see if anyone has any better suggestions.
但我只是想看看是否有人有更好的建议。
回答by Unknown
I think there's nothing wrong with using the [0] subscript, but sometimes people use the "throwaway" variable _. It's actually just like any other variable (with special usage in the console), except that some Python users decided to have it be "throwaway" as a convention.
我认为使用 [0] 下标没有错,但有时人们使用“一次性”变量 _。它实际上就像任何其他变量一样(在控制台中有特殊用法),除了一些 Python 用户决定将它作为一种约定“一次性”。
count, _ = get_count_and_price()
About the PyDev problem, you should just use the [0] subscript anyway. But if you really want to use _ the only solution is to disable the unused variable warnings if that bothers you.
关于 PyDev 问题,无论如何您都应该使用 [0] 下标。但是如果你真的想使用 _ 唯一的解决方案是禁用未使用的变量警告,如果这让你感到困扰。
回答by Alex Martelli
Using _
as severally proposed may have some issues (though it's mostly OK). By the Python style guidelines we use at work I'd normally use count, unused_price = ...
since pylint is configured to ignore assignments to barenames starting with unused_
(and warn on USE of any such barename instead!-). But I don't know how to instruct PyDev
to behave that way!
使用多个_
提议可能有一些问题(尽管大部分都可以)。根据我们在工作中使用的 Python 风格指南,我通常会使用,count, unused_price = ...
因为 pylint 被配置为忽略对以 开头的裸名的分配unused_
(并警告使用任何此类裸名!-)。但我不知道如何指示PyDev
要那样做!
回答by Brent Writes Code
If you go to the Eclipse -> Preferences… window, you can actually specify which variable names PyDev should ignore if they're unused (I'm looking at the newest PyDev 1.5.X).
如果您转到 Eclipse -> Preferences... 窗口,您实际上可以指定 PyDev 在未使用时应忽略哪些变量名称(我正在查看最新的 PyDev 1.5.X)。
If you go to PyDev -> Editor -> Code Analysis and look at the last field that says "Don't report unused variable if name starts with"
如果您转到 PyDev -> 编辑器 -> 代码分析并查看最后一个字段,该字段显示“如果名称以以下开头,则不要报告未使用的变量”
Enter whatever names you want in there and then use that name to restrict what variable names PyDev will ignore unused warnings for.
在那里输入您想要的任何名称,然后使用该名称来限制 PyDev 将忽略未使用警告的变量名称。
By default, it looks like PyDev will hide unused variable warnings for any variables that have names beginning with "dummy", "_", or "unused".
默认情况下,对于名称以“dummy”、“_”或“unused”开头的任何变量,PyDev 似乎会隐藏未使用的变量警告。
As @TokenMacGuy said below, I'd recommend against using just "_" because it has special meaning in certain scenarios in Python (specifically it's used in the interactive interpreter).
正如@TokenMacGuy 在下面所说,我建议不要只使用“_”,因为它在 Python 的某些场景中具有特殊含义(特别是在交互式解释器中使用)。
回答by S.Lott
We often do this.
我们经常这样做。
count, _ = get_count_and_price()
or this
或这个
count, junk = get_count_and_price()
回答by Lambda Fairy
I'd rather name it _price
instead, for these reasons:
_price
出于以下原因,我宁愿将其命名为:
It solves the conflict with gettext and the interactive prompt, which both use
_
It's easy to change back into
price
if you end up needing it later.As others have pointed out, the leading underscore already has a connotation of "internal" or "unused" in many languages.
它解决了与 gettext 和交互式提示的冲突,两者都使用
_
price
如果您以后需要它,很容易改回它。正如其他人指出的那样,前导下划线在许多语言中已经具有“内部”或“未使用”的含义。
So your code would end up looking like this:
所以你的代码最终会是这样的:
(count, _price) = get_count_and_price()
回答by CryingCyclops
I'll go after a Necromancer badge. :)
我会去找死灵法师徽章。:)
You said you're using PyDev. In PyDev (at least recent versions - I didn't check how far back), any variable name that starts with "unused" will be exempt from the Unused Variable warning. Other static analysis tool may still complain, though (pyflakes does - but it seems to ignore this warning in a tuple-unpacking context anyway).
你说你正在使用 PyDev。在 PyDev 中(至少是最近的版本——我没有检查多远),任何以“unused”开头的变量名都不会受到 Unused Variable 警告的影响。尽管如此,其他静态分析工具可能仍然会抱怨(pyflakes 确实如此——但它似乎在元组解包上下文中忽略了这个警告)。