Oh dear, I hope I got the title right. :)
How can one pass the **kwargs supplied to a wrapper-function definition, to another (enclosed) function call that it wraps. For example:
def wrapped_func(**kwargs):
# Do some preparation stuff here.
func('/path/to/file.csv', comma_separated_key=value_injected_here)
# Do some other stuff.
So for example, this call:
wrapped_func(error_bad_lines=True, sep=':', skip_footer=0, ...)
Should result in this:
func('/path/to/file.csv', error_bad_lines=True, sep=':', skip_footer=0, ...)
I've tinkered with a variety of approaches over the past couple of hours, but each exposed type-preservation vulnerabilities (for the values). I've not used this particular wrapper pattern before, and was wondering if the community could give some help. Thank you in advance.
**kwargs
is a dict, meaning you can use the double splat (**
) to unpack it as a list of keyword arguments. So your wrapper function could be like this:
def wrapped_func(**kwargs):
# Do some preparation stuff here.
func('/path/to/file.csv', **kwargs)
# Do some other stuff.
Why not simply merge the kwargs:
def func(*args, **kwargs):
print args
print kwargs
def wrapped_func(**kwargs):
# Do some preparation stuff here.
func('/path/to/file.csv', **dict(comma_separated_key='value_injected_here', **kwargs))
# Do some other stuff.
wrapped_func(error_bad_lines=True, sep=':', skip_footer=0)
# Outputs:
('/path/to/file.csv',)
{'skip_footer': 0, 'error_bad_lines': True, 'comma_separated_key': 'value_injected_here', 'sep': ':'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With