如何优雅地忽略 Python 函数的一些返回值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26251861/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:15:55  来源:igfitidea点击:

How to elegantly ignore some return values of a Python function?

pythonreturn-value

提问by PascalVKooten

Sometimes a function gives a return value which you'd just want to discard rather than send to the output stream. What would be the elegant way to handle this?

有时,函数会提供一个您只想丢弃而不是发送到输出流的返回值。处理这个问题的优雅方式是什么?

Note that we're talking about a function that returns something which you cannot change.

请注意,我们正在讨论一个返回一些您无法更改的内容的函数。

def fn():
    return 5

I personally have used nullbefore, but I'm looking for a more pythonic way:

我个人以前使用null过,但我正在寻找一种更 Pythonic 的方式:

null = fn()

采纳答案by Daniel Roseman

The standard way to show this is to assign the results you don't want to _. For instance, if a function returns two values but you only want the first, do this:

显示这一点的标准方法是分配您不想要的结果_。例如,如果一个函数返回两个值,但您只想要第一个值,请执行以下操作:

value, _ = myfunction(param)

Most Python linters will recognize the use of _and not complain about unused variables.

大多数 Python linter 会识别使用_而不是抱怨未使用的变量。

If you want to ignore all returns, then simply don't assign to anything; Python doesn't do anything with the result unless you tell it to. Printing the result is a feature in the Python shell only.

如果你想忽略所有的回报,那么就不要分配任何东西;除非你告诉它,否则 Python 不会对结果做任何事情。打印结果只是 Python shell 中的一项功能。

回答by David Stein

I really like the idea of assigning to null - but it does have an unexpected side-effect:

我真的很喜欢分配给 null 的想法 - 但它确实有一个意想不到的副作用:

>>> print divmod(5, 3)
(1, 2)
>>> x, null = divmod(5, 3)
>>> print null
2

For those who code mostly in Python, this is probably not an issue: null should be just as much a junk value as _.

对于那些主要用 Python 编码的人来说,这可能不是问题:null 应该和 _ 一样是垃圾值。

However, if you're switching between other languages like Javascript, it's entirely possible to accidentally write a comparison with null instead of None. Rather than spitting the usual error about comparing with an unknown variable, Python will just silently accept it, and now you're getting true and false conditions that you totally don't expect... this is the sort of error where you stare at the code for an hour and can't figure out wth is wrong, until it suddenly hits you and you feel like an idiot.

但是,如果您在 Javascript 等其他语言之间切换,则完全有可能不小心编写与 null 而不是 None 的比较。与其吐出关于与未知变量比较的常见错误,Python 只会默默地接受它,现在你得到了你完全没有预料到的真假条件……这就是你盯着的那种错误一个小时的代码,无法弄清楚是错误的,直到它突然击中你,你觉得自己像个白痴。

The obvious fix doesn't work, either:

明显的修复也不起作用:

>>> x, None = divmod(5, 3)
File "<stdin>", line 1
SyntaxError: cannot assign to None

...which is really disappointing. Python (and every other language on the planet) allows allof the parameters to be implicitlythrown away, regardless of how many variables are returned:

……真是令人失望。无论返回多少变量,Python(以及地球上的所有其他语言)都允许隐式丢弃所有参数:

>>> divmod(5, 3)

...but explicitlythrowing away parameters by assigning them to None isn't allowed?

...但不允许通过将参数分配给 None 来明确丢弃参数?

It feels kind of stupid of the Python interpreter to characterize assigning to None as a syntax error, rather than an intentional choice with an unambiguous result. Can anyone think of a rationale for that?

Python 解释器将赋值给 None 描述为语法错误,而不是具有明确结果的有意选择,这感觉有点愚蠢。任何人都可以想到这样做的理由吗?