Is there a way to send more kwargs into a function than is called for in the function call?
Example:
def mydef(a, b):
print a
print b
mydict = {'a' : 'foo', 'b' : 'bar'}
mydef(**mydict) # This works and prints 'foo' and 'bar'
mybigdict = {'a' : 'foo', 'b' : 'bar', 'c' : 'nooooo!'}
mydef(**mybigdict) # This blows up with a unexpected argument error
Is there any way to pass in mybigdict without the error? 'c' would never be used in mydef in my ideal world and would just be ignored.
Thanks, my digging has not come up with what I am looking for.
Edit: Fixed the code a bit. The mydef(a, b, **kwargs) was the form that I was looking for, but the inspect function args was a new thing to me and definitely something for my toolbox. Thanks everyone!
mydict = { 'a':'foo', 'b':'bar' }and pass that tomydef(**dict). Using dicts and (non)keyword arguments this way is not common. You'll sometimes see if it you defined mydef with default arguments :mydef(a=None, b=None), but rarely will you see it passed that way with regular arguments ... With regular arguments, you'll more frequently see:mytuple = ('foo','bar')and then the call uses the (single) splat operator:mydef(*mytuple)dicts instead oftuples? I haven't come across a case where I would ...tuplesto positional arguments, and I passdictsto default and keyword arguments. So, a typical function call of mine might look like:myfunc(*mytuple,**mydict)wherefuncis defined:def func(a,b,c=2,**kwargs):andmytuplemight be(1, 2). Apparently some people might do it differently (as you have been doing it) and MartijnPieters corroborates. You're right that with tuples, the order matters and the order is the order that you would pass the arguments to the positional arguments in the underlying function.