My question is related to this one, however the **kwargs
solution there isn't working for me.
Let's say I have some functions as shown below. func_a
prints the values of arg1
and arg2
. func_b
prints the values of arg1
and arg3
. So arg1
is common to both functions. I now have a function, func
, that calls both func_a
and func_b
. In the arguments to func
I want to be able to pass arg1
, with arg2
and arg3
as optional arguments, hence why I set arg2
and arg3
to None
by default. However, when I try to call func(arg1='how', arg2='are')
I get an unexpected keyword argument
error. How can I fix this?
def func_a(arg1, arg2=None):
print(arg1)
if arg2 is not None:
print(arg2)
def func_b(arg1, arg3=None):
print(arg1)
if arg3 is not None:
print(arg3)
def func(arg1, **kwargs):
func_a(arg1, **kwargs)
func_b(arg1, **kwargs)
# Try to call func
func(arg1='how', arg2='are')
how
are
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-372fbc7b1e90> in <module>
----> 1 func(arg1='how', arg2='are')
<ipython-input-8-5edc2a4c5bfd> in func(arg1, **kwargs)
11 def func(arg1, **kwargs):
12 func_a(arg1, **kwargs)
---> 13 func_b(arg1, **kwargs)
TypeError: func_b() got an unexpected keyword argument 'arg2'