【问题标题】:Python identity function and itself method [duplicate]Python标识函数及其自身方法[重复]
【发布时间】:2019-12-15 12:27:33
【问题描述】:

python 标准库中是否有任何函数返回其参数,类似于lambda x: x 和返回其自身的方法?

我的动机是实现类似的东西:

def identity(x):
    return x

def default(arg, default, func=identity):
    return func(arg) if arg is not None else func(default)

Ruby 有 itself,类似于:

class Self:
    def itself(self):
        return self

返回其参数或自身的函数/方法组合还有其他用例。

【问题讨论】:

  • 不,没有,但您似乎已经能够很容易地定义一个。
  • 问题问here好像不存在

标签: python


【解决方案1】:

恕我直言,有两个包将 identity 与许多其他功能性、纯 python 和高性能方法一起带来:funcytoolz

【讨论】:

    猜你喜欢
    • 2016-04-27
    • 2015-02-28
    • 2019-02-20
    • 1970-01-01
    • 2016-09-24
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多