Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Passing and updating default parameters idoms

Tags:

python

If I wrap a function into a bigger function but I still want to have access to all the parameters of the inner function is customary to do:

def bigFun(par1, **kwargs):
    innerFun(**kwargs)

Now, if I want to provide default values in the wrapper function and still have let the user to override these value I can do:

def bigFun(par1, **kwargs):
    default_kwargs = dict(keyX=valueX, keyY=valueY, ...)
    default_kwargs.update(**kwargs)
    kwargs = default_kwargs
    innerFun(**kwargs)

which I don't particularly like.

It seems a common enough situation to me.

Any other idiom do people use in this case?

like image 868
user2304916 Avatar asked Feb 13 '26 23:02

user2304916


1 Answers

def bigFun(par1, **kwargs):
    kwargs[keyX] = kwargs.get(keyX, valueX)
    innerFun(**kwargs)

or for multiple pairs:

def bigFun(par1, **kwargs):
    for k,v in [('key1', 'val1'), ('key2', 'val2')]:
        kwargs[k] = kwargs.get(k, v)
    innerFun(**kwargs)
like image 131
perreal Avatar answered Feb 15 '26 12:02

perreal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!