Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a dictionary to a function that accepts **kwargs?

Tags:

python

I have a function that accepts wildcard keyword parameters:

def func(**kargs):     doA     doB 

How do I send it a dictionary?

like image 469
flybywire Avatar asked Oct 13 '09 11:10

flybywire


People also ask

Can you pass a dictionary into Kwargs?

Passing Dictionary as an argument In Python, everything is an object, so the dictionary can be passed as an argument to a function like other variables are passed. “ kwargs ” stands for keyword arguments.

How do you use Kwargs in a function?

The double asterisk form of **kwargs is used to pass a keyworded, variable-length argument dictionary to a function. Again, the two asterisks ( ** ) are the important element here, as the word kwargs is conventionally used, though not enforced by the language.

How do you pass a dictionary argument in Robot Framework?

To pass a dictionary to a keyword, you do it like any other argument. In your case, if you have a dictionary named ${Participants} , you would pass it as ${Participants} . As for iterating over the dictionary, you need to replace $ with @ , and use FOR/IN.

How do you pass multiple dictionary parameters in Python?

Use *args to take multiple arguments. Show activity on this post. Show activity on this post. If you want to avoid combining the dictionaries, then provide them as arguments rather than keyword arguments, and set their defaults to something (such as None ), and convert None to {} and otherwise copy the dictionary.


1 Answers

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.

like image 115
nosklo Avatar answered Oct 12 '22 14:10

nosklo