python中一个合适的“什么都不做”的lambda表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22738412/
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
A suitable 'do nothing' lambda expression in python?
提问by user3002473
I sometimes find myself wanting to make placeholder 'do nothing' lambda expressions, similar to saying:
我有时发现自己想让占位符“什么都不做”的 lambda 表达式,类似于说:
def do_nothing(*args):
pass
But the following syntax is illegal since lambda expressions attempt to return whatever is after the colon, and you can't return pass
.
但是以下语法是非法的,因为 lambda 表达式试图返回冒号之后的任何内容,而您不能返回pass
.
do_nothing = lambda *args: pass
So I was wondering, would the following expression be a suitable replacement for the above?
所以我想知道,下面的表达是否适合替换上面的表达?
do_nothing = lambda *args: None
Since the do_nothing
function above technically returns None
, is it okay to make a lambda expression that returns None
to use as a placeholder lambda expression? Or is it bad practice?
由于上述do_nothing
函数在技术上返回None
,是否可以将返回的 lambda 表达式None
用作占位符 lambda 表达式?或者这是不好的做法?
采纳答案by Thanatos
This:
这个:
def do_nothing(*args):
pass
is equivalent to:
相当于:
lambda *args: None
With some minor differences in that one is a lambda
and one isn't. (For example, __name__
will be do_nothing
on the function, and <lambda>
on the lambda.) Don't forget about **kwargs
, if it matters to you. Functions in Python without an explicit return <x>
return None
. This is here:
有一些细微差别,一个是一个lambda
,一个不是。(例如,__name__
将do_nothing
在函数上,<lambda>
在 lambda 上。)不要忘记**kwargs
,如果它对您很重要。Python 中没有显式return <x>
return 的函数None
。这是在这里:
A call always returns some value, possibly None, unless it raises an exception.
调用总是返回一些值,可能是 None,除非它引发异常。
I've used similar functions as default values, say for example:
我使用了类似的函数作为默认值,例如:
def long_running_code(progress_function=lambda percent_complete: None):
# Report progress via progress_function.
回答by Paul
Sometimes lambda functions are used for data transformation, and in that case 'do nothing' means to return the input, i.e.
有时 lambda 函数用于数据转换,在这种情况下,“什么都不做”意味着返回输入,即
lambda x: x
lambda x: x
To return none you can write
要返回 none 你可以写
lambda x: None
lambda x: None
回答by Kyle Kelley
If you truly want a full do nothing function, make sure to take *args
and*kwargs
.
如果你真的想要一个完整的什么都不做的功能,一定要使用*args
和*kwargs
。
noop = lambda *args, **kwargs: None
In all its glorious action
在它所有光荣的行动中
>>> noop = lambda *args, **kwargs: None
>>> noop("yes", duck_size="horse", num_ducks=100)
>>>
Side Note
边注
Do yourself a favor for the future and include the **kwargs
handling. If you ever try to use it somewhere deep away in your code and you forgot that it doesn'ttake kwargs
, it will be quite the Exception to doing nothing:
为未来帮自己一个忙,包括**kwargs
处理。如果你曾经尝试使用它的地方,在你的代码深深隐藏起来,你忘了,它并不需要kwargs
,这将是相当异常无所事事:
In [2]: do_nothing('asdf', duck="yes")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-efbd722f297c> in <module>()
----> 1 do_nothing('asdf', duck="yes")
TypeError: <lambda>() got an unexpected keyword argument 'duck'
回答by Rafa0809
This is the simplest lambda expression:
这是最简单的 lambda 表达式:
do_nothing = lambda: None
No arguments required and the minimal required return.
不需要参数和最小的要求回报。
回答by Arthur Khazbs
In Python 3you don't even need to define any functions for that. Calling type(None)
will return you the NoneType
constructor, which you can use for doing nothing: type(None)()
. Keep in mind that the NoneType
constructor only takes 0 arguments.
在Python 3 中,您甚至不需要为此定义任何函数。调用type(None)
将返回NoneType
构造函数,您可以使用它什么都不做:type(None)()
. 请记住,NoneType
构造函数只接受 0 个参数。
In Python 2, though, creating instances of NoneType
is impossible, so lambda: None
would make the most sense.
但是,在Python 2 中,创建 的实例NoneType
是不可能的,因此lambda: None
最有意义。