I want to pass keyword arguments to integrand function in the dblquad or nquad. Is it possible at all to have a keyword argument here or should I just opt in for having positional arguments only?
Basically, I tried to pass dictionary as a normal argument. Below is my attempt at doing that:
def foo(A, B, **kwargs):
alpha = kwargs.get('alpha', 1.0)
beta = kwargs.get('beta', 1.0)
return A*alpha+B*beta
def integrator(**kwargs):
alpha = kwargs.get('alpha', 1.0)
beta = kwargs.get('beta', 1.0)
a = dblquad(foo, 0, 2*pi, lambda x: 0, lambda x: 2*pi, args=(kwargs))
integrator(alpha = 1.0, beta = 2.0)
Python complains about having an improper number of positional arguments. It treats keywords dictionary as a number of positional arguments.
Short answer: kwargs are not supported.
Possible workarounds include passing keyword args as positionals, passing a single dict as a positional argument, or attaching relevant keywords as attributes to the function you're integrating.