13

If I have a simple function:

def add(a, b, c):
    return a + b + c

Is it possible for me to make it so that if I supply an unused kwarg, it is simply ignored?

kwargs = dict(a=1, b=2, c=3, d=4)
print add(**kwargs) #prints 6
Lucretiel
  • 3,145
  • 1
  • 24
  • 52
  • I think that's the purpose of `**kwargs`... – BenDundee Mar 19 '13 at 21:38
  • 2
    Duplicate: [How does one ignore unexpected keyword arguments passed to a function?](https://stackoverflow.com/questions/26515595/how-does-one-ignore-unexpected-keyword-arguments-passed-to-a-function) [Newer, but has more detailed answers.] – Mateen Ulhaq Jan 09 '20 at 12:20

1 Answers1

29

Sure. Just add **kwargs to the function signature:

def add(a, b, c, **kwargs):
    return a + b + c

kwargs = dict(a=1, b=2, c=3, d=4)
print add(**kwargs) 
#prints 6
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677