我想指向一个什么也不做的函数:
def identity(*args)
return args
我的使用场景类似于这样
try:
gettext.find(...)
...
_ = gettext.gettext
else:
_ = identity
当然,我可以使用上面定义的identity
,但内置函数肯定会更快(而且避免了由自己引入的错误)。
显然,map
和filter
使用None
作为identity,但这是特定于它们的实现。
>>> _=None
>>> _("hello")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
map(None, [1, 2, 3])
的翻译是将函数map()
应用于参数[1, 2, 3]
,其中第一个参数为None
。 - Greg Hewgill