将包装函数定义中收到的** kwargs传递给封闭的(即包装的)函数调用的参数 [英] Passing **kwargs received in a wrapper-function definition, to arguments of an enclosed (i.e. wrapped) function call
问题描述
哦,亲爱的,我希望我能拿到冠军头衔.:)
Oh dear, I hope I got the title right. :)
如何将提供给包装器功能 定义的** kwargs传递给它包装的另一个(封闭的)函数 call .例如:
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.
例如,此调用:
wrapped_func(error_bad_lines=True, sep=':', skip_footer=0, ...)
应导致以下结果:
func('/path/to/file.csv', error_bad_lines=True, sep=':', skip_footer=0, ...)
在过去的几个小时中,我尝试了多种方法,但每个方法都暴露了 type-preservation 漏洞(针对这些值).我以前没有使用过这种特殊的包装模式,并且想知道社区是否可以提供帮助.预先谢谢你.
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
是dict,这意味着您可以使用double splat( **
)来将其解压缩为关键字参数列表.因此,您的包装函数可能是这样的:
**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.
这篇关于将包装函数定义中收到的** kwargs传递给封闭的(即包装的)函数调用的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!