1

I want to set defaults for my kwargs how would I do that?

def function(arg1, arg2, **kwargs, kwargs['a'] = 1):
    print(kwargs['a'])

function(1, 2)

Result would be 1.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
  • 2
    Why not just `a=1`? Or you could use [`.get`](https://stackoverflow.com/questions/1098549/proper-way-to-use-kwargs-in-python). – meowgoesthedog Jan 21 '19 at 15:16

1 Answers1

1

Define each default argument individually, then use kwargs for the rest:

def foo(arg1, arg2, a=1, **kwargs):
John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • Make it `def foo(arg1, arg2, *, a=1, **kwargs):` if you want to ensure `a` is *only* passed by keyword, matching how `**kwargs` would receive it (and you're on Python 3; not supported on Py2). – ShadowRanger Jan 21 '19 at 15:20
  • well I want the default and the kwargs in the same list but the defaults are over ridden. – Brent Trenholme Jan 21 '19 at 15:44