I have a function that accepts wildcard keyword parameters:
def func(**kargs):
doA
doB
How do I send it a dictionary?
I have a function that accepts wildcard keyword parameters:
def func(**kargs):
doA
doB
How do I send it a dictionary?
func(**mydict)
this will mean kwargs=mydict inside the function
all the keys of mydict must be strings
Just use func(**some_dict) to call it.
This is documented on section 4.7.4 of python tutorial.
Note that the same dict is not passed into the function. A new copy is created, so some_dict is not kwargs.
It's not 100% clear by your question, but if you'd like to pass a dict in through kwargs, you just make that dict a part of another dict, like so:
my_dict = {} #the dict you want to pass to func
kwargs = {'my_dict': my_dict } #the keyword argument container
func(**kwargs) #calling the function
Then you can catch my_dict in the function:
def func(**kwargs):
my_dict = kwargs.get('my_dict')
or...
def func(my_dict, **kwargs):
#reference my_dict directly from here
my_dict['new_key'] = 1234
I use the latter a lot when I have the same set of options passed to different functions, but some functions only use some the options (I hope that makes sense...). But there are of course a million ways to go about this. If you elaborate a bit on your problem we could most likely help you better.