It's possible to create a partial function in python with the partial
function from functools
, however doing so will usually change some parameters to be keyword-only parameters. For example:
def foo(a, b, c, d):
return a*b - c*d
bar = functools.partial(foo, b=2, c=3)
inspect.signature(foo)
# <Signature (a, b, c, d)>
inspect.signature(bar)
# <Signature (a, *, b=2, c=3, d)>
In the example, parameter a
keeps its original kind, but d
is keyword-only now. Is there a way to bind a function's parameters to some values while preserving the other parameters' kind? In other words, a way to make bar
from above functionally equivalent to:
def bar(a, d):
return a*2 - 3*d
but bar
could be any function, and any combination of parameters could be bound.