偏函数 partial
简化或重定义函数传参方式
1)在一些场景想简化函数传参个数
2)在使用一些三方接口时不允许回调函数有传参时,可以使用偏函数简化
比如在pika的连接对象中有很多回调的接口只能传入一个无参的回调函数,但是在这个回调函数被调用时是需要参数的,此时就可以使用偏函数了。
例如pika的这个接口需要传一个回调函数 callback,但是没有函数传参此时就可以使用偏函数进行简化了
def add_callback_threadsafe(self, callback):
pika源码给出的示例
For example, a thread may request a call to the
`BlockingChannel.basic_ack` method of a `BlockingConnection` that is
running in a different thread via::
connection.add_callback_threadsafe(
functools.partial(channel.basic_ack, delivery_tag=...))
这里消费者在手动回ack时需要delivery_tag,利用partia函数分装一个无参函数作为callback之后调用add_callback_threadsafe函数。
不指定入参顺序代码示例
from functools import partial
def sum_func(a, b):
print(f"a={a},b={b}")
return a + b
# 修改sum_func入参,a固定为10
new_sum_func = partial(sum_func, 10)
# 再次使用新函数只需要传入一个参数即可
res = new_sum_func(10)
print(f"sum={res}") # sum=20
# 修改sum_func入参,参数a和b固定
new_sum_func2 = partial(sum_func, 10, 20)
res = new_sum_func2()
print(f"sum={res}") # sum=30
指定入参顺序代码示例
from functools import partial
# 调用函数sum_func时*后面的参数必须全部指定参数名
# 直接调用会报sum_func() takes 1 positional argument but 3 were given
# *后指定一个参数名会报positional argument follows keyword argument
def sum_func(a, *, b, c):
print(f"a={a},b={b},c={c}")
return a + b + c
new_sum_func = partial(sum_func, 10, b=20)
# 参数名c必须加上,否则报错
# new_sum_func() takes 1 positional argument but 2 positional arguments (and 1 keyword-only argument) were given
res = new_sum_func(c=30)
print(f"sum={res}") # sum=60
new_sum_func = partial(sum_func, 10, b=20, c=30)
# 指定参数可以覆盖原来的值
res = new_sum_func(b=40)
print(f"sum={res}") # sum=80