Given a dictionary params={'a':0, 'b':1, 'c':2, 'd':3}
I want to pass them to a function foo
:
def foo(a, b, c=None, d=None):
pass
Something as simple as foo(**params)
would complain about mixing positional and named arguments.
There is an ugly way:
a = params['a']
b = params['b']
del params['a']
del params['b']
foo(a, b, **params)
But what I am ideally looking for is something like:
def separate(params: Dict, func: Callable) -> List[Any], Dict[str, Any]:
...
args, kwargs = separate(params, foo)
foo(*args, **kwargs)
Is there a nice way to do it or should I rely on inspect
and implement the separate
function myself here?