In the python documenation, it says:
在python文档中,它说:
Any function argument, no matter non-optional or optional (with default value) can be called as keyword argument as long as one of the argument names matches. Keyword argument, however, must follow all positional arguments.
任何函数参数,无论是非可选的还是可选的(具有默认值)都可以作为关键字参数调用,只要其中一个参数名称匹配即可。但是,关键字参数必须遵循所有位置参数。
I tried this out:
我试过这个:
kwargs = {'step':-1, 'start':10, 'stop':5}
list(range(**kwargs))
But python gives men an error:
但是python给了男人一个错误:
TypeError: range() takes no keyword arguments
Why is this?
为什么是这样?
1 个解决方案
#1
7
range()
is not a python function. It is a C type; C types follow different rules for arguments and range()
only accepts positional arguments.
range()不是python函数。它是C型; C类型遵循不同的参数规则,range()只接受位置参数。
See the Calls expressions documentation:
请参阅Calls表达式文档:
CPython implementation detail: An implementation may provide built-in functions whose positional parameters do not have names, even if they are ‘named’ for the purpose of documentation, and which therefore cannot be supplied by keyword. In CPython, this is the case for functions implemented in C that use
PyArg_ParseTuple()
to parse their arguments.CPython实现细节:实现可能提供内置函数,其位置参数没有名称,即使它们为了文档的目的而“命名”,因此也不能通过关键字提供。在CPython中,这是在C中实现的函数的情况,它使用PyArg_ParseTuple()来解析它们的参数。
The positional parameters of range()
are not named so cannot be used as keyword arguments.
range()的位置参数未命名,因此不能用作关键字参数。
#1
7
range()
is not a python function. It is a C type; C types follow different rules for arguments and range()
only accepts positional arguments.
range()不是python函数。它是C型; C类型遵循不同的参数规则,range()只接受位置参数。
See the Calls expressions documentation:
请参阅Calls表达式文档:
CPython implementation detail: An implementation may provide built-in functions whose positional parameters do not have names, even if they are ‘named’ for the purpose of documentation, and which therefore cannot be supplied by keyword. In CPython, this is the case for functions implemented in C that use
PyArg_ParseTuple()
to parse their arguments.CPython实现细节:实现可能提供内置函数,其位置参数没有名称,即使它们为了文档的目的而“命名”,因此也不能通过关键字提供。在CPython中,这是在C中实现的函数的情况,它使用PyArg_ParseTuple()来解析它们的参数。
The positional parameters of range()
are not named so cannot be used as keyword arguments.
range()的位置参数未命名,因此不能用作关键字参数。