作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有许多结合了位置参数和关键字参数的函数,我想将它们的一个参数绑定(bind)到给定值(只有在函数定义之后才知道)。有没有通用的方法?
我的第一次尝试是:
def f(a,b,c): print a,b,c
def _bind(f, a): return lambda b,c: f(a,b,c)
bound_f = bind(f, 1)
但是,为此我需要知道传递给 f
的确切参数,并且不能使用单个函数来绑定(bind)我感兴趣的所有函数(因为它们具有不同的参数列表)。
最佳答案
>>> from functools import partial
>>> def f(a, b, c):
... print a, b, c
...
>>> bound_f = partial(f, 1)
>>> bound_f(2, 3)
1 2 3
关于python - 如何将参数绑定(bind)到 Python 函数中的给定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3188048/
我是一名优秀的程序员,十分优秀!