将包装函数定义中收到的**kwargs传递给封闭(即包装)函数调用的参数

NYC*_*yes 4 python function kwargs

哦,亲爱的,我希望我的头衔正确.:)

如何将提供给包装函数 定义的**kwargs传递给它包装的另一个(封闭的)函数调用.例如:

def wrapped_func(**kwargs):
   # Do some preparation stuff here.
   func('/path/to/file.csv', comma_separated_key=value_injected_here)
   # Do some other stuff.
Run Code Online (Sandbox Code Playgroud)

例如,这个电话:

wrapped_func(error_bad_lines=True, sep=':', skip_footer=0, ...)
Run Code Online (Sandbox Code Playgroud)

应该导致:

func('/path/to/file.csv', error_bad_lines=True, sep=':', skip_footer=0, ...)
Run Code Online (Sandbox Code Playgroud)

在过去的几个小时里,我已经采用了各种方法,但每种方法都暴露了类型保留漏洞(对于价值观).我之前没有使用过这种特殊的包装模式,并且想知道社区是否可以提供一些帮助.先感谢您.

Nat*_*cat 6

**kwargs是一个字典,意味着您可以使用双splat(**)将其解压缩为关键字参数列表.所以你的包装函数可能是这样的:

def wrapped_func(**kwargs):
   # Do some preparation stuff here.
   func('/path/to/file.csv', **kwargs)
   # Do some other stuff.
Run Code Online (Sandbox Code Playgroud)