7
votes

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?

2
So whats the problem now? its very clear that range() takes no keyword arguments - kasravnd
Where did you find that text? I cannot find any reference of it other than this post. - Martijn Pieters♦
I have not been able to locate any text in the Python documentation, including the tutorial, that uses wording anywhere like what you quoted here. Can you please provide us with the source? - Martijn Pieters♦
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). People might hate me for suggesting this but I've being doing this for range since I have a terrible memory of what the order of things are. Imo this shouldn't be a problem so I'm fixing it: range(*{'start':0,'stop':10,'step':2}.values()) - Charlie Parker

2 Answers

21
votes

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.

2
votes

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())