python - 如何在 Python 中将并集/交集方法作为参数传递?

标签 python parameter-passing

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

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

最佳答案

方法是一等对象,就像其他函数一样。你可以将它传递给绑定(bind):

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

或未绑定(bind)

my_function(set.union)

根据需要。

例子:

def test(s1, s2, op):
    return op(s1,s2)

test(set([1]), set([2]), set.union)  # set([1, 2])

关于python - 如何在 Python 中将并集/交集方法作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34856650/

相关文章:

reporting-services - 过滤数据集中的多值参数

python - 在包含字符串和整数的列表上使用 max()

python - XML ElementTree - 索引标签

c++,函数指针+函数模板

c - 使用系统命令传递参数

perl - 为什么从@_ 转移的值不是别名?

grails - Grails-无法在空对象上获取属性 'id'

python - Pandas 分组和过滤器

python - 具有虚拟输入的简单模型中具有 Softmax 交叉熵的 NaN

python - Pymongo 错误服务器超时