【问题标题】:How can I pass union/intersection methods as a parameter in Python?如何在 Python 中将联合/交集方法作为参数传递?
【发布时间】:2016-01-18 14:05:38
【问题描述】:

我可以将Set Python 类的并集和交集方法作为参数传递吗?

我正在寻找直接的方法来做到这一点,而不使用额外的 lambda 或常规函数。

【问题讨论】:

    标签: python parameter-passing


    【解决方案1】:

    方法是一等对象,就像其他函数一样。你可以通过绑定:

    x = set([1,2,3])
    my_function(x.union)
    

    或未绑定

    my_function(set.union)
    

    根据需要。

    例子:

    def test(s1, s2, op):
        return op(s1,s2)
    
    test(set([1]), set([2]), set.union)  # set([1, 2])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 2017-10-12
      • 2019-02-11
      • 1970-01-01
      相关资源
      最近更新 更多