The way you did it is fine. Another way is for funB
to have the same defaults as funA
, so you can pass the same parameters right through. E.g., if you do def funB(x, a, b=1)
, then you can always call return funA(x, a, b)
just like that.
For simple cases, the above will work fine. For more complex cases, you may want to use *args
and **kwargs
(explained here and here). Specifically, you can pass in all your keyword arguments as a dictionary (conventionally called kwargs
). In this case, each function would set its own independent defaults, and you would just pass the whole dictionary through:
def funA(x, a, **kwargs):
b = kwargs.get("b", 1)
return a+b*x
def funB(x, a, **kwargs):
return funA(x, a, **kwargs)
If kwargs
is empty when passed to funB
(b
is not specified), it will be set to the default in funA
by the statement b = kwargs.get("b", 1)
. If b
is specified, it will be passed through as-is. Note that in funB
, you can access b
with its own, independent default value and still get the behavior you are looking for.
While this may seem like overkill for your example, extracting a couple of arguments at the beginning of a function is not a big deal if the function is complex enough. It also gives you a lot more flexibility (such as avoiding many of the common gotchas).