I'd like to call a function in python using a dictionary.
Here is some code:
d = dict(param='test') def f(param): print(param) f(d)
This prints {'param': 'test'}
but I'd like it to just print test
.
I'd like it to work similarly for more parameters:
d = dict(p1=1, p2=2) def f2(p1, p2): print(p1, p2) f2(d)
Is this possible?
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.
Use the Python **kwargs parameter to allow the function to accept a variable number of keyword arguments. Inside the function, the kwargs argument is a dictionary that contains all keyword arguments as its name-value pairs. Precede double stars ( ** ) to a dictionary argument to pass it to **kwargs parameter.
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.
In essence, one can say that mutable objects like dictionaries, sets, and lists are passed by reference. Immutable objects like int , str , tuple are passed by value.
Figured it out for myself in the end. It is simple, I was just missing the ** operator to unpack the dictionary
So my example becomes:
d = dict(p1=1, p2=2) def f2(p1,p2): print p1, p2 f2(**d)
In[1]: def myfunc(a=1, b=2): In[2]: print(a, b) In[3]: mydict = {'a': 100, 'b': 200} In[4]: myfunc(**mydict) 100 200
A few extra details that might be helpful to know (questions I had after reading this and went and tested):
Examples:
Number 1: The function can have parameters that are not included in the dictionary
In[5]: mydict = {'a': 100} In[6]: myfunc(**mydict) 100 2
Number 2: You can not override a function parameter that is already in the dictionary
In[7]: mydict = {'a': 100, 'b': 200} In[8]: myfunc(a=3, **mydict) TypeError: myfunc() got multiple values for keyword argument 'a'
Number 3: The dictionary can not have values that aren't in the function.
In[9]: mydict = {'a': 100, 'b': 200, 'c': 300} In[10]: myfunc(**mydict) TypeError: myfunc() got an unexpected keyword argument 'c'
How to use a dictionary with more keys than function arguments:
A solution to #3, above, is to accept (and ignore) additional kwargs in your function (note, by convention _
is a variable name used for something being discarded, though technically it's just a valid variable name to Python):
In[11]: def myfunc2(a=None, **_): In[12]: print(a) In[13]: mydict = {'a': 100, 'b': 200, 'c': 300} In[14]: myfunc2(**mydict) 100
Another option is to filter the dictionary based on the keyword arguments available in the function:
In[15]: import inspect In[16]: mydict = {'a': 100, 'b': 200, 'c': 300} In[17]: filtered_mydict = {k: v for k, v in mydict.items() if k in [p.name for p in inspect.signature(myfunc).parameters.values()]} In[18]: myfunc(**filtered_mydict) 100 200
Example with both positional and keyword arguments:
Notice further than you can use positional arguments and lists or tuples in effectively the same way as kwargs, here's a more advanced example incorporating both positional and keyword args:
In[19]: def myfunc3(a, *posargs, b=2, **kwargs): In[20]: print(a, b) In[21]: print(posargs) In[22]: print(kwargs) In[23]: mylist = [10, 20, 30] In[24]: mydict = {'b': 200, 'c': 300} In[25]: myfunc3(*mylist, **mydict) 10 200 (20, 30) {'c': 300}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With