python - python 中的 kwargs 保留字。这是什么意思?

标签 python keyword-argument

<分区>

我正在使用 Python 试图找出一个关键字,我看到了“kwargs”这个词,我知道它是被调用函数中的某种参数,但我找不到它是什么表示或代表任何地方。

例如,Python 文档中的这个条目说...

read_holding_registers(address, count=1, **kwargs)

参数:

address – The starting address to read from
count – The number of registers to read
unit – The slave unit this request is targeting

它看起来像是对指向指针的指针的引用,但仅此而已 可以告诉...

这甚至不在参数列表中使用“**kwargs” 它使用 在我看来,“unit”而不是“kwargs”。

我似乎无法在任何地方找到kwargs 的含义。

也许是“关键字参数”?我在这里错过了什么?

关于帮助的任何想法? 谢谢你 ! 鲍勃

最佳答案

**kwargs 表示关键字参数。它实际上不是 python 关键字,它只是人们遵循的约定。这将获取传递给函数的所有键值参数。例如,

def func(*args, **kwargs):
    print args, kwargs
func(1, "Welcome", name="thefourtheye", year=2013)

将打印

(1, 'Welcome') {'name': 'thefourtheye', 'year': 2013}

关于python - python 中的 kwargs 保留字。这是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20158516/

相关文章:

python - 为什么 ** 不在函数调用中解压 kwargs?

python - 数据类参数是否有别名或名称参数?

python将本地时间的字符串转换为UTC纪元时间戳

python - 创建接受 kwargs 的 str(或 int 或 float 或 tuple)的 child

python - 通过列表旋转找到最小绝对差总和的最快算法

python + 导入 C++ 编码帮助。 MSVCCompiler 缺少 compiler_cxx 修复程序;无法找到 vcvarsall.bat

python - 创建 GAE 实体时遇到问题

python - 在 Python 中使用 **kwargs 的正确方法

python - 从 concurrent.futures 到 asyncio

python - 在函数中创建的变量是否具有全局范围?