Is there a way in Python to pass explicitly a dictionary to the **kwargs
argument of a function? The signature that I'm using is:
def f(*, a=1, **kwargs): pass # same question with def f(a=1, **kwargs)
I tried to call it the following ways:
my_dict=dict(b=2)
f(kwargs=my_dict) # wrong, kwargs receives {'kwargs': {'b': 2}} instead of {'b': 2}
f(**kwargs=my_dict) # SyntaxError: invalid syntax
f(kwargs=**my_dict) # SyntaxError: invalid syntax
The reason I want to do this is that the dictionary I pass in may contain the key a
, and I don't want it to pollute the f
's argument a
:
overlap_dict=dict(a=3,b=4)
f(**overlap_dict) # wrong, 'a' is 3, and 'kwargs' only contains 'b' key
f(a=2, **overlap_dict) # TypeError: f() got multiple values for keyword argument 'a'
Replacing **kwargs
with a dictionary in f
's signature is not an option for me.