Let’s create a simple tuple, dictionary and function.
>>> tup = (7, 3)
>>> dic = {"kw1":7, "kw2":3}
>>> def pr(a, b):
... print a, b
The following shows what * does before a tuple and dictionary as an argument.
>>> pr(tup)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pr() takes exactly 2 arguments (1 given)
>>> pr(*tup)
7 3
>>> pr(*dic)
kw1 kw2
Now let’s try ** before an argument.
>>> pr(**tup)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pr() argument after ** must be a mapping, not tuple
Ok, it seems ** only works when a dictionary is used as an argument. So let’s try it with a dictionary then.
>>> pr(**dic)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pr() got an unexpected keyword argument 'kw1'
What?
Can anybody show me an example of this last case that doesn’t produce an error?
Your
prfunction takes argumentsaandb, notkw1andkw2.