Question

I use python3.3 and just found out that it accepts keyword arguments in some of its CPython functions:

>>> "I like python!".split(maxsplit=1)
['I', 'like python!']

But some other functions don't accept keyword arguments:

>>> sum([1,2,3,4], start = 10)
Traceback (most recent call last):
  File "<pyshell#58>", line 1, in <module>
    sum([1,2,3,4], start = 10)
TypeError: sum() takes no keyword arguments

My question is: what is the difference between those functions? Which functions in CPython accept keyword arguments, which functions don't? And of course - why?

Was it helpful?

Solution

CPython functions that use PyArg_ParseTuple() to parse their arguments do not support keyword arguments (mostly because PyArg_ParseTuple() only supports positional parameters, e.g. a simple sequence).

This is explained in the CPython implementation details here:

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top