python - Creating a partial function that sets a keyword argument in the original function -
not sure if possible i'm trying create partial function function positional , keyword arguments. problem is, want argument in resulting partial function set 1 of keyword arguments in original function - not 1 of positional arguments.
here original function's definition:
def cost_function(self, x, y, weights=none, lambda_param=0.0):
i want partial function can pass scipy.minimise can find optimum weights values (weights ndarray).
so need partial function 1 argument (x say):
e.g.
cost_func(x)
but want partial function set weights argument when calls original function:
my_network.cost_function(x, y, weights=x, lambda_param=0.0)
(i know change original function weights argument positional not keyword argument don't want because want use function without weights argument set).
i think functools.partial meet needs
see example:
import functools def cost_function(self, x, y, weights=none, lambda_param=0.0): print (weights) cost_func = functools.partial(cost_function, none, 20, 30, lambda_param=2.0) print (cost_func("yay worked!!!"))
Comments
Post a Comment