且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 **kwargs 将 **kwargs 参数传递给另一个函数

更新时间:2023-11-09 23:35:04

在第二个示例中,您提供 3 个参数:文件名、模式和字典 (kwargs).但是 Python 需要:2 个形式参数加上关键字参数.

In the second example you provide 3 arguments: filename, mode and a dictionary (kwargs). But Python expects: 2 formal arguments plus keyword arguments.

通过在字典前加上 '**' 前缀,您可以将字典 kwargs 解包为关键字参数.

By prefixing the dictionary by '**' you unpack the dictionary kwargs to keywords arguments.

字典(类型 dict)是包含键值对的单个变量.

A dictionary (type dict) is a single variable containing key-value pairs.

关键字参数"是键值方法参数.

"Keyword arguments" are key-value method-parameters.

任何字典都可以通过在函数调用期间以 ** 为前缀来解压为关键字参数.

Any dictionary can by unpacked to keyword arguments by prefixing it with ** during function call.