You can create partial functions in python by using the partial function from the functools library.
Partial functions allow one to derive a function with x parameters to a function with fewer parameters and fixed values set for the more limited function.
官方文档functools.partial(func, /, *args, **keywords)
返回一个新的 部分对象,当被调用时其行为类似于 func 附带位置参数 args 和关键字参数 keywords 被调用。 如果为调用提供了更多的参数,它们会被附加到 args。 如果提供了额外的关键字参数,它们会扩展并重载 keywords。
使用示例from functools import partial
def power(x: int, y: int) -> int:
return x ** y
if __name__ == '__main__':
func0 = partial(power, 2)
func1 = partial(power, x=2) # be careful
func2 = partial(power, y=2)
>>> func0(3) 8 >>> func1(3) # not allow TypeError: power() got multiple values for argument 'x' >>> func1(y=3) 8 >>> func2(3) Out[5]: 9



