In the python documenation, it says:
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:
TypeError: range() takes no keyword arguments
Why is this?
range()
is not a python function. It is a C type; C types follow different rules for arguments and range()
only accepts positional arguments.
See the Calls expressions documentation:
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.
The positional parameters of range()
are not named so cannot be used as keyword arguments.
As you know by now the real answer is that range
is a C function which for some reason does not have the same rules of python (would be nice to know why).
But you can do this instead:
range(*{'start':0,'stop':10,'step':2}.values())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With