Python的保留字。这是什么意思?

2024-05-29 02:55:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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的意思。

也许是“关键词争论”?我错过了什么?

有什么帮助的建议吗? 谢谢您! 鲍勃


Tags: theto文档read参数addresscountunit
2条回答

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

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

将打印

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

是的,它的意思是“关键字参数”,但是kwargs实际上不是一个保留字。调用参数来收集所有未使用的关键字参数只是一种习惯用法。

相关问题 更多 >

    热门问题