在Python3中是否有一个用于函数lambda x: x的内置函数?有时,它可能会使一些代码更优雅。例如:
def my_sort(lst, key):
...
lst = [4, 3, 7, 1, 9]
my_sort(lst, key=lambda x: x) # probably it can be written in a better way WITHOUT lambda x: x with the help of a standard function我需要一个内置的或Python3中的标准函数,而不是像lambda x: x这样的表达式来改进我的代码。
发布于 2017-11-08 19:45:59
一种pythonic方法是使用None的默认参数,使key成为可选的:
def my_sort(lst, key=None):
if key is None:
key = lambda x: x
...
lst = [4, 3, 7, 1, 9]
my_sort(lst)https://stackoverflow.com/questions/47178582
复制相似问题