Reputation: 957
A similar question has been asked before but I don't know that I like the answer. You have a parent function, which calls a child function.
def parent(a):
return child(a) * 2
def child(a, b=10, c=20):
return a + b + c
If I want the parent method to expose b
and c
, I can do something like below. It works, but seems cumbersome, as I'm reaching for it a lot (maybe that suggests some other problem).
def parent(a, b=None, c=None):
kwargs = {}
if b is not None:
kwargs['b']=b
if c is not None:
kwargs['c']=c
return child(a, **kwargs)
Is there a better (i.e. less code) way to do this?
Upvotes: 3
Views: 683
Reputation: 169
If parent and child are both your own functions, then you could have the default values defined as a constant that is shared for both functions.
B_DEF = 10
C_DEF = 20
def parent(a, b=B_DEF, c=C_DEF):
return child(a,b,c) * 2
def child(a, b=B_DEF, c=C_DEF):
return a + b + c
Upvotes: 3