Search code examples
pythonkeyword-argument

Passing more kwargs into a function than initially set


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!


Solution

  • To clarify Martijn Pieters's answer (for sake of clarity). It's possible if you change the function signature to:

    def mydef(a, b, **kwargs):

    This means it's not possible without changing the signature. But if that's not a problem it'll work.